//AC #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=998244353; //xのy乗 ll mod_pow(ll x,ll y,ll mod){ ll power=x; ll ret = 1; while(y>0){ if(y&1){ ret = ret*power%mod; } power = power*power%mod; y = y>>1; } return ret; } struct combination{ typedef long long ll; std::vector fact,ifact; combination(ll n): fact(n+1),ifact(n+1){ fact[0]=1;//0!=1通り for(ll i=1;i<=n;i++) fact[i] = fact[i-1]*i%MOD; //ifact[n] = fact[n].inv(); // n!の逆元がifact[n] ifact[n] = mod_pow(fact[n],MOD-2,MOD);// n!を(mod-2)乗した後でmodであまりをとった for(ll i=n;i>=1;i--) ifact[i-1] = ifact[i]*i%MOD; } ll operator()(ll n,ll k){ if(k<0 || k>n) return 0; // n!/( k!*(n-k)! ) return (fact[n]*ifact[k])%MOD * ifact[n-k]%MOD; } }; //転倒数を数えるために用いる template struct binary_indexed_tree{ int N; vector bit; binary_indexed_tree(int n):N(n+1){ bit = vector(n+1,0); N=n; } void add(int x,Type a){ x++;//1から始めるための補正 //for(int i=x; i<=N; i+=(i&-i)) bit[i] = addition(bit[i],a); while(x<=N){ //bit[x] = addition(bit[x],a); bit[x] =bit[x]+a; x += x&-x; } } Type sum(int x){ x++;//1から始まることに注意 Type ret=0; //for(int i=x; i>0; i-=(i&-i)) ret = addition(ret,bit[i]); while(x>0){ ret = ret+bit[x]; x -= x&-x; } return ret; } //[l,r]の範囲 // rはN-1以下 Type get(int l,int r){ if(r>N) return 0;//配列の外へのアクセス if(l>r) return 0;//本来は l<=r となるのでおかしい if(l==0) return sum(r);//[0,r]//ここでoutなわけか else return (sum(r) - sum(l-1)); } }; combination comb(2*100000+10); //転倒数のカウントに用いる ll calc(vector arg){ ll res=0; //segment_tree seg(); const int MAXN=1e5; binary_indexed_tree bit(MAXN+5); //順列に出現する順番で見ていく for(int v:arg){ //vよりも大きい値で インデックスがvよりも小さい数 res+=bit.get(v+1,MAXN+1); bit.add(v,1); } return res; }; int main(){ int N,M; cin>>N>>M; vector> PM(M); vector P(M),K(M); //vector L(N,1);//L[i]=順列の0~i番目までで,空きがある場所 vector R(N,1);//R[i]=順列のi~N-1番目までで,空きがある場所 for(int i=0;i>P[i]>>K[i]; P[i]--;K[i]--; //数字P[i]が数列のK[i]番目にある PM[i]=make_pair(P[i],K[i]); } ll ans=0; { ll cnt=0; //X,Yのどちらも固定されている場合 sort(PM.begin(),PM.end(), [](const pair& lhs,const pair& rhs){ return lhs.second ord; for(auto [p,k]:PM){//数字のみを取り出す ord.push_back(p); } ll res=calc(ord); res%=MOD; //(N-M)!をかける cnt=res*comb.fact[N-M]%MOD; ans+=cnt; ans%=MOD; } { //Xのみ順列の指定位置.Yの指定がない vector L(N,1);//L[i]=順列の0~i番目までで,空きがある場所 vector usednum;//すでに条件で出現した数字 for(int i=0;i=0){ Li=L[k-1]; } //usednumのうちの数字pより大きいものは何個あるか //[,usednum.end()) int num = usednum.end() - upper_bound(usednum.begin(),usednum.end(),p); //p+1~N-1のうち, num個が使われている. //(Yの選び方) =[p+1,N-1]-num = (N-1)-(p+1)+1 - num ll cnt=(max(0,N-p-1-num))%MOD; cnt*=Li%MOD; cnt%=MOD; if(N-M-1>=0){ cnt*=comb.fact[N-M-1]; cnt%=MOD; }else{ cnt=0; } ans+=cnt; ans%=MOD; } } { //Yのみ順列が固定 Xは固定されない vector R(N,1);//R[i]=順列のi~N-1番目までで,空きがある場所 vector usednum; for(int i=0;i=0;i--){ R[i]=R[i]+R[i+1]; } //Yのみ順列の指定の位置. for(int i=0;i=0){ cnt*=comb.fact[N-M-1]; cnt%=MOD; }else{ cnt=0; } ans+=cnt; ans%=MOD; } } { //X,Yのどちらも指定がない場合.ただし X=0){ fa=comb.fact[N-M-2]; } cnt=cnt*fa%MOD; ans+=cnt; ans%=MOD; } cout<