n, p = map(int, input().split()) pcnt = 0 pnow = p while pnow <= n: pcnt += n // pnow pnow *= p mod = 10 ** 9 + 7 def bikkuri_pre(n): global bikkuri, irukkib bikkuri = [1] irukkib = [1] for i in range(1, n + 1): bikkuri.append((bikkuri[-1] * i) % mod) for i in range(1, n + 1): irukkib.append(pow(bikkuri[i], mod - 2, mod)) bikkuri_pre(n) print(pcnt * pow(bikkuri[n], bikkuri[n], mod) % mod)