N,P = map(int,input().split()) mod = 10 ** 9 + 7 ans = 0 p = P while p <= N: ans += N // p p *= P tmp = 1 for i in range(2,N + 1): tmp = tmp * i % mod #print(tmp) for i in range(1,N + 1): tmp = pow(tmp,i,mod) #print(tmp) print(ans * tmp % mod)