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 fc = math.factorial(n) print(nfact_cnt * pow(fc % MOD, fc, MOD) % MOD)