It is a permutation of the subject.
Observed up to a total of N! Permutations embodiment, the first keyword, if there are the same elements a1, a2 ... so bad permutation C1 = a1 * a2 * ... identical to the second key, obtains C2 . However, if the total of minus two kinds of bad permutation is clearly incorrect, because it is possible to delete the duplicate arrangement, then put this together like a repeat, is the first keyword orderly, also ordered the second keyword . The final ans = N! -C1-C2 + C3.
Write your own time should be in modulo bombed, and found a very good writing. for reference
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int maxn=3e5+5; 5 const ll mod=998244353; 6 7 pair <int,int > a[maxn]; 8 ll d[maxn]; 9 ll fi[maxn],se[maxn]; 10 map<pair<int,int>,int> M; 11 int n; 12 ll sum=1;ll e=1,b=1,c=1; 13 int main(){ 14 cin>>n; 15 d[0]=1; 16 for(int i=1;i<=n;i++){ 17 cin>>a[i].first>>a[i].second; 18 d[i]=(d[i-1]*i)%mod; 19 e=e*(++fi[a[i].first])%mod; 20 b=b*(++se[a[i].second])%mod; 21 c=c*(++M[a[i]])%mod; 22 } 23 sort(a+1,a+n+1); 24 25 int now=a[1].second;int sign=1; 26 for(int i=2;i<=n;i++){ 27 if(a[i].second>=now) now=a[i].second; 28 else 29 { 30 sign=0; 31 break; 32 } 33 } 34 ll ans; 35 if(sign) 36 { 37 ans=(d[n]+c+2*mod-e-b)%mod; 38 } 39 else ans=(d[n]+2*mod-e-b)%mod; 40 cout <<ans<<"\n"; 41 42 43 return 0; 44 }