N, P = map(int, input().split()) MOD = 10**9 + 7 count = 0 now = 1 total = 1 for i in range(1, N+1): total *= i total %= MOD total = pow(total, total, MOD) while P**now <= N: count += N // (P**now) now += 1 count *= total print(count % MOD)