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