//https://yukicoder.me/problems/no/2326 #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; const ll INF=1LL<<60; typedef pair P; typedef pair PP; const ll MOD=1e9+7; ll mod_pow(ll x,ll y, ll mod){ ll res=1; while(y>0){ if(y&1){ res*=x; res%=mod; } x*=x; x%=mod; y/=2; } return res; } ll Legendre(ll n, ll p,ll mod){ ll res=0; ll q=p; while (1) { ll d=(n/q)%mod; if(d==0) break; res+=d; res%=mod; q=q*p; } return res; } //n!のmod ll factorial(ll n,ll mod){ ll res=1; for(ll t=1;t<=n;t++){ res*=t%mod; res%=mod; } return res; } int main(){ ll N,P; cin>>N>>P; //N!の中のPで割れる個数 ll cnt=Legendre(N,P,MOD); /* while(n>0){ cnt+=(n/P)%MOD; cnt%=MOD; n/=P; } */ //N!をMOD-1で割ったあまり ll p=factorial(N,MOD-1); /* ll k=p; for(ll t=N;t>=1;t--){ k=mod_pow(k,t,MOD-1); } */ ll e=factorial(N,MOD); ll k=mod_pow(p,e,MOD); //cout<<"cnt="<