MOD = 10 ** 9 + 7 n, p = map(int, input().split()) fact = [1 for _ in range(n + 1)] for x in range(2, n + 1): fact[x] = (fact[x - 1] * x) % MOD dp = [0 for _ in range(n + 1)] for x in range(2, n + 1): cur = x while cur % p == 0: dp[x] += 1 cur //= p ans = sum(dp) % MOD * pow(fact[n], fact[n], MOD) % MOD print(ans)