CF D. Number Of Permutations 排列

时间:2023-03-10 04:51:56
CF D. Number Of Permutations 排列

挺水的一道题~

拿全排列随便乘一下就好了.

#include <cstdio>
#include <algorithm>
#define N 300004
#define ll long long
#define mod 998244353
#define setIO(s) freopen(s".in","r",stdin)
using namespace std;
struct Node {
int a,b;
}t[N];
ll fac[N];
bool cmp1(Node a,Node b) {
return a.a==b.a?a.b<b.b:a.a<b.a;
}
bool cmp2(Node a,Node b) {
return a.b==b.b?a.a<b.a:a.b<b.b;
}
int main() {
int i,j,n;
ll tot1=1,tot2=1,tot3=1;
// setIO("input");
fac[0]=1;
for(i=1;i<N;++i) fac[i]=1ll*fac[i-1]*i%mod;
scanf("%d",&n);
for(i=1;i<=n;++i) scanf("%d%d",&t[i].a,&t[i].b);
sort(t+1,t+1+n,cmp1);
for(i=1;i<=n;i=j) {
for(j=i;j<=n&&t[j].a==t[i].a;++j);
tot1=tot1*fac[j-i]%mod;
}
sort(t+1,t+1+n,cmp2);
for(i=1;i<=n;i=j) {
for(j=i;j<=n&&t[j].b==t[i].b;++j);
tot2=tot2*fac[j-i]%mod;
}
int flag=0;
for(i=2;i<=n;++i) if(t[i].a<t[i-1].a) flag=1;
if(flag) printf("%I64d\n",(fac[n]-(tot1+tot2)%mod+mod)%mod);
else {
for(i=1;i<=n;i=j) {
for(j=i;j<=n&&t[j].a==t[i].a&&t[j].b==t[i].b;++j);
tot3=tot3*fac[j-i]%mod;
}
printf("%I64d\n",(fac[n]-(tot1+tot2-tot3+mod)%mod+mod)%mod);
}
return 0;
}