#include using namespace std; void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); } #include using mint = atcoder::modint1000000007; const int MOD = 1e9 + 7; int main() { fast_io(); int n, p; cin >> n >> p; int cnt = 0; { int pp = p; while (n >= pp) { cnt += n / pp; if (pp > INT_MAX / p) break; pp *= p; } } mint ans = 1; long long fact = 1; for (int i = 2; i <= n; i++) { ans *= i; fact *= i; fact %= (MOD - 1); } ans = ans.pow(fact) * cnt; cout << ans.val() << endl; }