MOD = 10 ** 9 + 7 N, P = map(int, input().split()) a = 0 k = 1 while 1: div = N // (P ** k) if div == 0: break a += div k += 1 b = 1 for i in range(1, N + 1): b *= i b %= MOD - 1 c = 1 for i in range(1, N + 1): c *= i c %= MOD - 2 print(a, b, c) ans = a * pow(b, c) % MOD print(ans)