N, P = map(int, input().split()) MOD = 10**9 + 7 fact = 1 for i in range(1, N + 1): fact *= i fact %= MOD fact2 = 1 for i in range(1, N + 1): fact2 *= i fact2 %= MOD - 1 val = pow(fact, fact2, MOD) cnt, tmp = 0, P while tmp <= N: cnt += N // tmp tmp *= P ans = cnt * val % MOD print(ans)