import sys input = sys.stdin.readline N,P=map(int,input().split()) mod=10**9+7 now=1 for i in range(1,N+1): now=now*i%(mod-1) x=now now=1 for i in range(1,N+1): now=now*i%mod y=now ANS=0 for i in range(1,31): ANS+=N//pow(P,i) if pow(P,i)>N: break k=pow(y,x,mod) print(ANS*k%mod)