MOD = 10**9 + 7 N, P = map(int, input().split()) # Compute e: exponent of P in N! using Legendre's formula sum_e = 0 current_p = P while current_p <= N: sum_e += N // current_p current_p *= P if sum_e == 0: print(0) else: # Compute a = N! mod MOD a = 1 for i in range(2, N + 1): a = (a * i) % MOD # Compute b = N! mod (MOD-1) mod_m1 = MOD - 1 b = 1 for i in range(2, N + 1): b = (b * i) % mod_m1 # Compute a^b mod MOD power = pow(a, b, MOD) # Calculate the final answer ans = (sum_e * power) % MOD print(ans)