import math MOD = 10 ** 9 + 7 N,P = map(int, input().split()) # るしゃんどる fst = 0 fst_p = P while True: x = N // fst_p if x == 0:break fst += x fst_p *= P #print(f"{fst=}") # N! mod P snd = 1 for i in range(1,N+1): snd *= i snd %= MOD if snd == 0: snd = MOD #print(f"{snd=}") thd = math.factorial(N) #print(f"{thd=}") ans = fst * pow(snd,thd,MOD) print(ans % MOD)