MOD = 10 ** 9 + 7 N, P = map(int, input().split()) a = 0 k = 1 while 1: div = N // (P ** k) if div == 0: break a += div k += 1 b = 1 for i in range(1, N + 1): b *= i b %= MOD - 1 c = 1 for i in range(1, N + 1): c *= i c %= MOD - 1 # print(a, b, c) ans = a * pow(b, c, MOD) % MOD print(ans)