codeforces1027D. Number Of Permutations(排列组合+容斥原理)

题目链接

http://codeforces.com/contest/1207/problem/D

题目描述

You are given a sequence of n pairs of integers: (a1,b1),(a2,b2),…,(an,bn). This sequence is called bad if it is sorted in non-descending order by first elements or if it is sorted in non-descending order by second elements. Otherwise the sequence is good. There are examples of good and bad sequences:

  • s= [(1,2),(3,2),(3,1)] is bad because the sequence of first elements is sorted: [1,3,3];
  • s= [(1,2),(3,2),(1,2)] is bad because the sequence of second elements is sorted: [2,2,2];
  • s= [(1,1),(2,2),(3,3)] is bad because both sequences (the sequence of first elements and the sequence of second elements) are sorted;
  • s= [(1,3),(3,3),(2,2)] is good because neither the sequence of first elements ([1,3,2]) nor the sequence of second elements ([3,3,2]) is sorted.

Calculate the number of permutations of size nn such that after applying this permutation to the sequence ss it turns into a good sequence.

A permutation p of size nn is a sequence p1,p2,…,pn consisting of nn distinct integers from 1 to n (1≤pi≤n). If you apply permutation p1,p2,…,pn to the sequence s1,s2,…,sn you get the sequence sp1,sp2,…,spn. For example, if s=[(1,2),(1,3),(2,3)] and p=[2,3,1] then s turns into [(1,3),(2,3),(1,2)].

Input

The first line contains one integer nn (1≤n≤3⋅10^5).

The next nn lines contains description of sequence ss. The ii-th line contains two integers aiai and bibi (1≤ai,bi≤n) — the first and second elements of ii-th pair in the sequence.

The sequence ss may contain equal elements.

Output

Print the number of permutations of size nn such that after applying this permutation to the sequence ss it turns into a good sequence. Print the answer modulo 998244353 (a prime number).

Examples

input

3
1 1
2 2
3 1

output

3

input

4
2 3
2 2
2 1
2 4

output

0

input

3
1 1
1 1
2 3

output

4

Note

In first test case there are six permutations of size 33:

  1. if p=[1,2,3], then s=[(1,1),(2,2),(3,1)] — bad sequence (sorted by first elements);
  2. if p=[1,3,2], then s=[(1,1),(3,1),(2,2)] — bad sequence (sorted by second elements);
  3. if p=[2,1,3], then s=[(2,2),(1,1),(3,1)] — good sequence;
  4. if p=[2,3,1], then s=[(2,2),(3,1),(1,1)] — good sequence;
  5. if p=[3,1,2], then s=[(3,1),(1,1),(2,2)] — bad sequence (sorted by second elements);
  6. if p=[3,2,1], then s=[(3,1),(2,2),(1,1)] — good sequence.

题意

 n个二维数对(ai,bi),求将这n个数对排列之后,ai,bi都不是单调不减的。这样的排列有多少个。

思路

根据容斥原理,答案=总排列数-ai单调不减-bi单调不减+ai与bi均单调不减。具体计算不难,高中排列组合知识。

代码

#include<bits/stdc++.h>
#define ll long long
#define maxn 300010
#define mod 998244353
using namespace std;
struct node{
	ll a,b;
}te[maxn];
bool cmp1(node x,node y){
	return x.a<y.a;
}
bool cmp2(node x,node y){
	return x.b<y.b;
}
bool cmp(node x,node y){
	if(x.a==y.a)return x.b<y.b;
	return x.a<y.a;
}

ll n;
ll jiecheng(ll x){
	ll an=1,i;
	for(i=1;i<=x;i++){
		an*=i;
		an%=mod;
	}
	return an;
}

int main(){
	int i,j;
	scanf("%lld",&n);
	for(i=1;i<=n;i++){
		scanf("%lld%lld",&te[i].a,&te[i].b);
	}
	ll sa=1,sb=1,ss=1,all=1;
	all=jiecheng(n);
	sort(te+1,te+1+n,cmp1);
	ll cnt=1;
	for(i=2;i<=n;i++){
		if(te[i].a!=te[i-1].a){
			sa*=jiecheng(cnt);sa%=mod;			
			cnt=1;
		}
		else cnt++;
	}
	sa*=jiecheng(cnt);sa%=mod;
	
	cnt=1;
	sort(te+1,te+1+n,cmp2);
	for(i=2;i<=n;i++){
		if(te[i].b!=te[i-1].b){
			sb*=jiecheng(cnt);sb%=mod;			
			cnt=1;
		}
		else cnt++;
	}
	sb*=jiecheng(cnt);sb%=mod;	
	cnt=1;
	sort(te+1,te+1+n,cmp);
	for(i=2;i<=n;i++)if(te[i].b<te[i-1].b)ss=0;
	
	for(i=2;i<=n;i++){
		if(te[i].b!=te[i-1].b||te[i].a!=te[i-1].a){
			ss*=jiecheng(cnt);ss%=mod;			
			cnt=1;
		}
		else cnt++;
	}
	ss*=jiecheng(cnt);ss%=mod;
	ll ans=(all-sa-sb+ss+mod+mod)%mod;
	printf("%lld\n",ans);
    return 0;
}
发布了27 篇原创文章 · 获赞 11 · 访问量 3593

猜你喜欢

转载自blog.csdn.net/Megurine_Luka_/article/details/100156418