#include using namespace std; // using namespace __gnu_pbds; // #include // using Bint = boost::multiprecision::cpp_int; #include using namespace atcoder; // https://atcoder.github.io/ac-library/production/document_ja/ typedef long long int ll; typedef long double ld; constexpr ll mod = 1e9+7; constexpr ll INF = 9'223'372'036'854'775'807/10; #define rep(i,n) for (ll i = 0; i < ll(n); ++i) #define Rep(i,a,n) for (ll i = (a); i < ll(n); ++i) #define All(a) (a).begin(),(a).end() #define Pi acos(-1) using V = vector; using P = pair; vector dx = {1, 0, -1, 0, 1, 1, -1, -1}; vector dy = {0, 1, 0, -1, 1, -1, 1, -1}; templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (b>; struct IoSetup { IoSetup() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << setprecision(15) << fixed; } } iosetup; void print(vector &v) { for (string s : v) { cout << s << '\n'; } } template void print(vector &v, int w = 0) { for (int i = 0; i < (int)v.size(); i++) { cout << right << setw(w) << v[i] << " \n"[i == (int)v.size() - 1]; } } template void print(vector> &v, int w = 0) { for (int i = 0; i < (int)v.size(); i++) { print(v[i], w); } } template void print(const T& arg) { cout << arg << '\n'; } template void print(const T& arg, const Args&... args) { cout << arg << ' '; print(args...); } __int128_t pow_mod_128(__int128_t A, __int128_t N, __int128_t M) { __int128_t res = 1 % M; A %= M; while (N) { if (N & 1) res = (res * A) % M; A = (A * A) % M; N >>= 1; } return res; } bool is_prime(long long N) { if (N <= 1) return false; if (N == 2) return true; if (N % 2 == 0) return false; vector A = {2, 325, 9375, 28178, 450775, 9780504, 1795265022}; long long s = 0, d = N - 1; while (d % 2 == 0) { ++s; d >>= 1; } for (auto a : A) { if (a % N == 0) return true; long long t, x = pow_mod_128(a, d, N); if (x != 1) { for (t = 0; t < s; ++t) { if (x == N - 1) break; x = __int128_t(x) * x % N; } if (t == s) return false; } } return true; } long long pollard(long long N) { if (N % 2 == 0) return 2; if (is_prime(N)) return N; auto f = [&](long long x) -> long long { return (__int128_t(x) * x + 1) % N; }; long long step = 0; while (true) { ++step; long long x = step, y = f(x); while (true) { long long p = gcd(y - x + N, N); if (p == 0 || p == N) break; if (p != 1) return p; x = f(x); y = f(f(y)); } } } vector prime_factorize(long long N) { if (N == 1) return {}; long long p = pollard(N); if (p == N) return {p}; vector left = prime_factorize(p); vector right = prime_factorize(N / p); left.insert(left.end(), right.begin(), right.end()); sort(left.begin(), left.end()); return left; } vector> prime_factorize_pair(long long N) { vector left = prime_factorize(N); left.push_back(-1); vector> g; long long cnt = 1; for (long long i = 1; i < left.size(); i++) { if (left[i] == left[i-1]) { cnt++; } else { g.push_back({left[i-1], cnt}); cnt = 1; } } return g; } using mint = modint1000000007; int main() { ll n, p; cin >> n >> p; auto g = prime_factorize_pair(p); mint ans = 0; mint b = 1; rep(i,n) { b *= (i+1); } mint c = b.pow(b.val()); rep(i, g.size()) { ll a = 0; ll pc = g[i].first; while (true) { a += n/pc; pc *= p; if (n/pc == 0) break; } ans += (a*c).val()/(g[i].second); } print(ans.val()); }