Problem:
Score : 1200 points

Problem Statement

There are 2N balls in the xy-plane. The coordinates of the i-th of them is (x_i, y_i).
Here, x_i and y_i are integers between 1 and N (inclusive) for all i, and no two balls occupy the same coordinates.

In order to collect these balls, Snuke prepared 2N robots, N of type A and N of type B.
Then, he placed the type-A robots at coordinates (1, 0), (2, 0), ..., (N, 0), and the type-B robots at coordinates (0, 1), (0, 2), ..., (0, N), one at each position.

When activated, each type of robot will operate as follows.

When a type-A robot is activated at coordinates (a, 0), it will move to the position of the ball with the lowest y-coordinate among the balls on the line x = a, collect the ball and deactivate itself. If there is no such ball, it will just deactivate itself without doing anything.

When a type-B robot is activated at coordinates (0, b), it will move to the position of the ball with the lowest x-coordinate among the balls on the line y = b, collect the ball and deactivate itself. If there is no such ball, it will just deactivate itself without doing anything.

Once deactivated, a robot cannot be activated again. Also, while a robot is operating, no new robot can be activated until the operating robot is deactivated.

When Snuke was about to activate a robot, he noticed that he may fail to collect all the balls, depending on the order of activating the robots.

Among the (2N)! possible orders of activating the robots, find the number of the ones such that all the balls can be collected, modulo 1 000 000 007.

Constraints

2 \leq N \leq 10^5

1 \leq x_i \leq N

1 \leq y_i \leq N

If i ≠ j, either x_i ≠ x_j or y_i ≠ y_j.

Inputs

Input is given from Standard Input in the following format:

N
x_1 y_1
...
x_{2N} y_{2N}

Outputs

Print the number of the orders of activating the robots such that all the balls can be collected, modulo 1 000 000 007.

Sample Input 1

2
1 1
1 2
2 1
2 2

Sample Output 1

8

We will refer to the robots placed at (1, 0) and (2, 0) as A1 and A2, respectively, and the robots placed at (0, 1) and (0, 2) as B1 and B2, respectively.
There are eight orders of activation that satisfy the condition, as follows:

A1, B1, A2, B2

A1, B1, B2, A2

A1, B2, B1, A2

A2, B1, A1, B2

B1, A1, B2, A2

B1, A1, A2, B2

B1, A2, A1, B2

B2, A1, B1, A2

Thus, the output should be 8.

Sample Input 2

4
3 2
1 2
4 1
4 2
2 2
4 4
2 1
1 3

Sample Output 2

7392

Sample Input 3

4
1 1
2 2
3 3
4 4
1 2
2 1
3 4
4 3

Sample Output 3

4480

Sample Input 4

8
6 2
5 1
6 8
7 8
6 5
5 7
4 3
1 4
7 6
8 3
2 8
3 6
3 2
8 5
1 5
5 8

Sample Output 4

82060779

Sample Input 5

3
1 1
1 2
1 3
2 1
2 2
2 3

Sample Output 5

0

When there is no order of activation that satisfies the condition, the output should be 0.

Solution in C++ (GCC 5.4.1) (judge verdict: Runtime Error)

#include <cstdio>
#include <vector>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define pb push_back
#define ref(i,x,y)for(int i=x;i<=y;++i)
#define def(i,x,y)for(int i=x;i>=y;--i)
typedef long long LL;
int read(){
	char c=getchar();int d=0,f=1;
	for(;c<'0'||c>'9';c=getchar())if(c=='-')f=-1;
	for(;c>='0'&&c<='9';d=d*10+c-48,c=getchar());
	return d*f;
}
const int N=200100,mod=1e9+7;
bool instk[N];
int n,tp,top,tmpr,ans,w[N],res,f[N],mkx[N],mky[N],F[N],vis[N],stk[N],h[N],t[N];
struct xint{int x,y;}p[N];
vector<int> vx[N],vy[N];

int cnt,head[N];
struct Xint{int to,next;}V[N*8];
void add(int x,int y){
	V[++cnt]=(Xint){y,head[x]};head[x]=cnt;
	V[++cnt]=(Xint){x,head[y]};head[y]=cnt;
}

bool cmpx(int a,int b){return p[a].x<p[b].x;}
bool cmpy(int a,int b){return p[a].y<p[b].y;}
void nosolution(){
	printf("0\n");exit(0);
}
void dfs(int f,int x){
	if(instk[x]){
		for(int i=top;;i--){
			h[++h[0]]=stk[i];
			if(stk[i]==x)break;
		}return;
	}
	if(vis[x])return;
	if(!mkx[p[x].x])tmpr++;mkx[p[x].x]=1;
	if(!mky[p[x].y])tmpr++;mky[p[x].y]=1;
	tmpr--;
	vis[x]=1;
	stk[++top]=x;instk[x]=1;
	for(int i=head[x];i;i=V[i].next){
		int y=V[i].to;if(y!=f)dfs(x,y);
	}
	stk[top--]=0;instk[x]=0;
}
int dfs(int x){
	vis[x]=tp;
	t[x]=0;
	int X=p[x].x,Y=p[x].y,sum=1;
	if(w[x]==1){
		int sz=vx[X].size();
		ref(i,0,sz-1)if(vx[X][i]!=x&&vis[vx[X][i]]!=tp){
			int y=vx[X][i];w[y]=2;
			int s=dfs(y);
			if(p[y].y<Y){
				t[x]+=t[y];
				sum=(LL)sum*F[t[y]]%mod*s%mod;
			}else res=(LL)res*F[t[y]]%mod*s%mod;
		}
	}
	if(w[x]==2){
		int sz=vy[Y].size();
		ref(i,0,sz-1)if(vy[Y][i]!=x&&vis[vy[Y][i]]!=tp){
			int y=vy[Y][i];w[y]=1;
			int s=dfs(y);
			if(p[y].x<X){
				t[x]+=t[y];
				sum=(LL)sum*F[t[y]]%mod*s%mod;
			}else res=(LL)res*F[t[y]]%mod*s%mod;
		}
	}
	sum=(LL)sum*f[t[x]]%mod;
	t[x]++;
	return sum;
}
void work(int x){
	tmpr=0;h[0]=0;dfs(0,x);
	if(tmpr!=0)nosolution();
	int o=0,xx,yy,s=0,Res=0;res=1;
	ref(i,1,h[0])if(!o||xx<p[h[i]].x||xx==p[h[i]].x&&yy<p[h[i]].y)
		o=h[i],xx=p[h[i]].x,yy=p[h[i]].y;
	tp=2;w[o]=1;s=dfs(o);Res=(Res+(LL)res*F[t[o]]%mod*s%mod)%mod;res=1;
	tp=3;w[o]=2;s=dfs(o);Res=(Res+(LL)res*F[t[o]]%mod*s%mod)%mod;
	ans=(LL)ans*Res%mod;
}
int mi(int a,int b){
	int s=1;
	for(;b;b>>=1,a=(LL)a*a%mod)
		if(b&1)s=(LL)s*a%mod;
	return s;
}
void init(){
	f[0]=F[0]=1;
	ref(i,1,n)f[i]=(LL)f[i-1]*i%mod;
	F[n]=mi(f[n],mod-2);
	def(i,n-1,1)F[i]=(LL)F[i+1]*(i+1)%mod;
}
int main(){
	n=read();n=n*2;
	ref(i,1,n)p[i].x=read(),p[i].y=read();
	ref(i,1,n)vx[p[i].x].pb(i),vy[p[i].y].pb(i);
	ref(i,1,n/2)if(!vx[i].empty()){
		sort(vx[i].begin(),vx[i].end(),cmpy);
		int s=vx[i].size();
		ref(j,0,s-2)add(vx[i][j],vx[i][j+1]);
	}
	ref(i,1,n/2)if(!vy[i].empty()){
		sort(vy[i].begin(),vy[i].end(),cmpx);
		int s=vy[i].size();
		ref(j,0,s-2)add(vy[i][j],vy[i][j+1]);
	}
	init();ans=f[n];
	ref(i,1,n)if(!vis[i])work(i);
	printf("%d\n",ans);
}