#include #include using namespace std; using namespace atcoder; #define all(...) std::begin(__VA_ARGS__), std::end(__VA_ARGS__) #define rall(...) std::rbegin(__VA_ARGS__), std::rend(__VA_ARGS__) #define OVERLOAD_REP(_1, _2, _3, _4, name, ...) name #define REP1(n) for(ll i=0;i=0;i--) #define RREP2(i, n) for(ll i=n-1;i>=0;i--) #define RREP3(i, a, n) for(ll i=n-1;i>=a;i--) #define RREP4(i, a, b, n) for(ll i=n-1;i>=a;i-=b) #define rrep(...) OVERLOAD_RREP(__VA_ARGS__, RREP4, RREP3, RREP2, RREP1)(__VA_ARGS__) #define foa(a,v) (auto& a : (v)) #define uniq(a) sort(all(a));a.erase(unique(all(a)),end(a)) #define len(n) (long long)(n).size() #define pb push_back using ll = long long; using ld = long double; using ull = unsigned long long; using vi = vector; using vvi = vector; using vvvi = vector; using vll = vector; using vvll = vector; using vvvll = vector; using vs = vector; using vvs = vector; using vvvs = vector; using vld = vector; using vvld = vector; using vvvld = vector; using vc = vector; using vvc = vector; using vvvc = vector; using pll = pair; using vpll = vector; template constexpr auto min(T... a){ return min(initializer_list>{a...}); } template constexpr auto max(T... a){ return max(initializer_list>{a...}); } template void input(T&... a){ (cin >> ... >> a); } ll POW(ll a,ll b){ ll ans = 1; while (b){ if (b & 1){ ans *= a; } a *= a; b /= 2; } return ans; } ll MODPOW(ll a,ll b,ll c){ ll ans = 1; while (b){ if (b & 1){ ans *= a; ans %= c; } a *= a; a %= c; b /= 2; } return ans; } #define OVERLOAD_POW(_1, _2, _3, name, ...) name #define INT(...) int __VA_ARGS__; input(__VA_ARGS__) #define LL(...) ll __VA_ARGS__; input(__VA_ARGS__) #define ULL(...) ull __VA_ARGS__; input(__VA_ARGS__) #define LD(...) ld __VA_ARGS__; input(__VA_ARGS__) #define STR(...) string __VA_ARGS__; input(__VA_ARGS__) #define CHA(...) char __VA_ARGS__; input(__VA_ARGS__) #define VLL(name,length) vll name(length);rep(i,length){cin >> name[i];} #define VVLL(name,h,w) vvll name(h,vll(w));rep(i,h)rep(j,w){cin >> name[i][j];} #define VVVLL(name,a,b,c) vvvll name(a,vvll(b,vll(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];} #define VI(name,length) vi name(length);rep(i,length){cin >> name[i];} #define VVI(name,h,w) vvi name(h,vi(w));rep(i,h)rep(j,w){cin >> name[i][j];} #define VVVI(name,a,b,c) vvvi name(a,vvll(b,vi(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];} #define VLD(name,length) vld name(length);rep(i,length){cin >> name[i];} #define VVLD(name,h,w) vvld name(h,vld(w));rep(i,h)rep(j,w){cin >> name[i][j];} #define VVVLD(name,a,b,c) vvvld name(a,vvld(b,vld(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];} #define VC(name,length) vc name(length);rep(i,length){cin >> name[i];} #define VVC(name,h,w) vvc name(h,vc(w));rep(i,h)rep(j,w){cin >> name[i][j];} #define VVVC(name,a,b,c) vvvc name(a,vvc(b,vc(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];} #define VS(name,length) vs name(length);rep(i,length){cin >> name[i];} #define VVS(name,h,w) vvs name(h,vs(w));rep(i,h)rep(j,w){cin >> name[i][j];} #define VVVS(name,a,b,c) vvvs name(a,vvs(b,vs(c)));rep(i,a)rep(j,b)rep(k,c){cin >> name[i][j][k];} #define PLL(name) pll name;cin>>name.first>>name.second; #define VPLL(name,length) vpll name(length);rep(i,length){cin>>name[i].first>>name[i].second;} void print(){cout << "\n";} template void print(const T& a, const Ts&... b){cout << a;(cout << ... << (cout << ' ', b));cout << '\n';} void print(vll x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}} void print(vvll x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}} void print(vi x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}} void print(vvi x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}} void print(vvvi x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << " ";}else if(j!=len(x[i])-1){cout << " | ";}else{cout << '\n';}}} void print(vld x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}} void print(vvld x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}} void print(vvvld x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << " ";}else if(j!=len(x[i])-1){cout << " | ";}else{cout << '\n';}}} void print(vc x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}} void print(vvc x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}} void print(vvvc x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << " ";}else if(j!=len(x[i])-1){cout << " | ";}else{cout << '\n';}}} void print(vs x){rep(i,len(x)){cout << x[i];if(i!=len(x)-1){cout << " ";}else{cout << '\n';}}} void print(vvs x){rep(i,len(x))rep(j,len(x[i])){cout << x[i][j];if(j!=len(x[i])-1){cout << " ";}else{cout << '\n';}}} void print(vvvs x){rep(i,len(x))rep(j,len(x[i]))rep(k,len(x[i][j])){cout << x[i][j][k];if(k!=len(x[i][j])-1){cout << " ";}else if(j!=len(x[i])-1){cout << " | ";}else{cout << '\n';}}} void print(pll x){cout << x.first << x.second << '\n';} void print(vpll x){rep(i,len(x)){cout << x[i].first << x[i].second << '\n';}} #include #include #include using namespace std; typedef long long ll; typedef pair Pii; #define FOR(i,n) for(int i = 0; i < (n); i++) #define sz(c) ((int)(c).size()) #define ten(x) ((int)1e##x) template T extgcd(T a, T b, T& x, T& y) { for (T u = y = 1, v = x = 0; a;) { T q = b / a; swap(x -= q * u, u); swap(y -= q * v, v); swap(b -= q * a, a); } return b; } template T mod_inv(T a, T m) { T x, y; extgcd(a, m, x, y); return (m + x % m) % m; } ll mod_pow(ll a, ll n, ll mod) { ll ret = 1; ll p = a % mod; while (n) { if (n & 1) ret = ret * p % mod; p = p * p % mod; n >>= 1; } return ret; } template class NTT { public: int get_mod() const { return mod; } void _ntt(vector& a, int sign) { const int n = sz(a); assert((n ^ (n&-n)) == 0); //n = 2^k const int g = 3; //g is primitive root of mod int h = (int)mod_pow(g, (mod - 1) / n, mod); // h^n = 1 if (sign == -1) h = (int)mod_inv(h, mod); //h = h^-1 % mod //bit reverse int i = 0; for (int j = 1; j < n - 1; ++j) { for (int k = n >> 1; k >(i ^= k); k >>= 1); if (j < i) swap(a[i], a[j]); } for (int m = 1; m < n; m *= 2) { const int m2 = 2 * m; const ll base = mod_pow(h, n / m2, mod); ll w = 1; FOR(x, m) { for (int s = x; s < n; s += m2) { ll u = a[s]; ll d = a[s + m] * w % mod; a[s] = u + d; if (a[s] >= mod) a[s] -= mod; a[s + m] = u - d; if (a[s + m] < 0) a[s + m] += mod; } w = w * base % mod; } } for (auto& x : a) if (x < 0) x += mod; } void ntt(vector& input) { _ntt(input, 1); } void intt(vector& input) { _ntt(input, -1); const int n_inv = mod_inv(sz(input), mod); for (auto& x : input) x = x * n_inv % mod; } // 畳み込み演算を行う vector convolution(const vector& a, const vector& b){ int ntt_size = 1; while (ntt_size < sz(a) + sz(b)) ntt_size *= 2; vector _a = a, _b = b; _a.resize(ntt_size); _b.resize(ntt_size); ntt(_a); ntt(_b); FOR(i, ntt_size){ (_a[i] *= _b[i]) %= mod; } intt(_a); return _a; } }; ll garner(vector mr, int mod){ mr.emplace_back(mod, 0); vector coffs(sz(mr), 1); vector constants(sz(mr), 0); FOR(i, sz(mr) - 1){ // coffs[i] * v + constants[i] == mr[i].second (mod mr[i].first) を解く ll v = (mr[i].second - constants[i]) * mod_inv(coffs[i], mr[i].first) % mr[i].first; if (v < 0) v += mr[i].first; for (int j = i + 1; j < sz(mr); j++) { (constants[j] += coffs[j] * v) %= mr[j].first; (coffs[j] *= mr[i].first) %= mr[j].first; } } return constants[sz(mr) - 1]; } typedef NTT<167772161, 3> NTT_1; typedef NTT<469762049, 3> NTT_2; typedef NTT<1224736769, 3> NTT_3; //任意のmodで畳み込み演算 O(n log n) vector int32mod_convolution(vector a, vector b,int mod){ for (auto& x : a) x %= mod; for (auto& x : b) x %= mod; NTT_1 ntt1; NTT_2 ntt2; NTT_3 ntt3; auto x = ntt1.convolution(a, b); auto y = ntt2.convolution(a, b); auto z = ntt3.convolution(a, b); vector ret(sz(x)); vector mr(3); FOR(i, sz(x)){ mr[0].first = ntt1.get_mod(), mr[0].second = (int)x[i]; mr[1].first = ntt2.get_mod(), mr[1].second = (int)y[i]; mr[2].first = ntt3.get_mod(), mr[2].second = (int)z[i]; ret[i] = garner(mr, mod); } return ret; } // garnerのアルゴリズムを直書きしたversion,速い vector fast_int32mod_convolution(vector a, vector b,int mod){ for (auto& x : a) x %= mod; for (auto& x : b) x %= mod; NTT_1 ntt1; NTT_2 ntt2; NTT_3 ntt3; assert(ntt1.get_mod() < ntt2.get_mod() && ntt2.get_mod() < ntt3.get_mod()); auto x = ntt1.convolution(a, b); auto y = ntt2.convolution(a, b); auto z = ntt3.convolution(a, b); // garnerのアルゴリズムを極力高速化した const ll m1 = ntt1.get_mod(), m2 = ntt2.get_mod(), m3 = ntt3.get_mod(); const ll m1_inv_m2 = mod_inv(m1, m2); const ll m12_inv_m3 = mod_inv(m1 * m2, m3); const ll m12_mod = m1 * m2 % mod; vector ret(sz(x)); FOR(i, sz(x)){ ll v1 = (y[i] - x[i]) * m1_inv_m2 % m2; if (v1 < 0) v1 += m2; ll v2 = (z[i] - (x[i] + m1 * v1) % m3) * m12_inv_m3 % m3; if (v2 < 0) v2 += m3; ll constants3 = (x[i] + m1 * v1 + m12_mod * v2) % mod; if (constants3 < 0) constants3 += mod; ret[i] = constants3; } return ret; } //2^23より大きく,primitive rootに3を持つもの // const int mods[] = { 1224736769, 469762049, 167772161, 595591169, 645922817, 897581057, 998244353 }; void ntt_test() { NTT_1 ntt; vector v; FOR(i, 16) v.push_back(10 + i); auto v2 = v; ntt.ntt(v2); auto v3 = v2; ntt.intt(v3); assert(v == v3); } void comvolution_test() { NTT_1 ntt1; vector v = { 1, 2, 3 }; vector u = { 4, 5, 6 }; auto vu = ntt1.convolution(v, u); vector vu2 = { 1 * 4, 1 * 5 + 2 * 4, 1 * 6 + 2 * 5 + 3 * 4, 2 * 6 + 3 * 5, 3 * 6, 0, 0, 0 }; assert(vu == vu2); } void int32mod_convolution_test(){ vector x , y; FOR(i, 10) x.push_back(ten(8) + i); y = x; auto z = int32mod_convolution(x, y, ten(9) + 7); z.resize(sz(x) + sz(y) - 1); vector z2 = { 930000007, 60000000, 390000001, 920000004, 650000003, 580000006, 710000014, 40000021, 570000042, 300000064, 370000109, 240000144, 910000175, 380000187, 650000193, 720000185, 590000162, 260000123, 730000074 }; assert(z == z2); } void test(){ ntt_test(); comvolution_test(); int32mod_convolution_test(); } using mint = modint; int main(){ LL(n,m,p); modint::set_mod(p); deque a; rep(i,1,n+1){ a.push_back({1,i}); } while(len(a) >= 2){ auto x = a.front(); a.pop_front(); auto y = a.front(); a.pop_front(); a.push_back(int32mod_convolution(x,y,p)); } mint ans = 0; int o = 10000; vector f(o,0); vector finv(o,0); f[1] = 1; finv[1] = mint(1).inv(); f[0] = 1; finv[0] = 1; rep(i,2,o){ f[i] = f[i-1]*i; finv[i] = finv[i-1] * mint(i).pow(p-2); } rep(i,1,m+1){ ans += a.front()[i] * f[n-i] * finv[m-i] * finv[n-i-(m-i)] * f[m] * finv[i]; } ans *= f[n-m] * finv[n]; print(ans.val()); }