import math MOD = 10**9 + 7 n, p = map(int, input().split()) nfact_cnt = 0 tmp = n while tmp > 0: nfact_cnt += tmp // p tmp = tmp // p fact = [1] * 101010 r = 1 for i in range(1, 101010): fact[i] = r = r * i % MOD fc = math.factorial(n) print(nfact_cnt * pow(fact[n], fc, MOD) % MOD)