N, P = map(int, input().split()) cnt = 0 now = P while N // now: cnt += N // now now *= P a, x = 1, 1 mod = 10**9 + 7 for i in range(1, N + 1): a *= i x *= i a %= mod x %= mod - 1 print(cnt * pow(a, x, mod) % mod)