MOD = 10 ** 9 + 7 N, P = map(int, input().split()) fact1 = [1] * (N + 1) fact2 = [1] * (N + 1) r1 = 1 r2 = 1 cnt = 0 for i in range(1, N + 1): fact1[i] = r1 = r1 * i % MOD fact2[i] = r2 = r2 * i % (MOD - 1) n = i while n % P == 0: cnt += 1 n //= P ans = (cnt * pow(fact1[N], fact2[N], MOD)) % MOD print(ans)