using namespace std; #include void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); cout< inline bool chmax(ll& a, ll b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(ll& a, ll b) { if (a > b) { a = b; return 1; } return 0; } #define rep(i, star,fini) for (int i = star; i < fini; i++) #define ALL(x) std::begin(x), std::end(x) #define INF ((1LL<<62)-(1LL<<31)) #define bit(x,i)(((x)>>(i))&1) long double distance(long double xi,long double yi,long double xj,long double yj){return sqrt(1.0*((xi-xj)*(xi-xj))+1.0*((yi-yj)*(yi-yj)));} ll MOD = 1000000007; long long modpow(long long a, long long n) { ll mod = MOD; long long res = 1; while (n > 0) { if (n & 1) res = res * a % mod; a = a * a % mod; n >>= 1; } return res; } void _main(){ ll n,p;cin >> n >> p; ll cnt = 0; ll x = n; while (x>0) { cnt += x/p; cnt%=MOD; x/=p; } ll n_f=1; rep(i,1,n+1){ (n_f*=i)%=MOD; } cout<<(modpow(n_f,n_f)*cnt)%MOD; }