#include<bits/stdc++.h>
#define mo 1000000007
#define pi 3.1415926535898
#define eps 1e-9 
using namespace std;
long long read(){
    long long xx=0,flagg=1;
    char ch=getchar();
    while((ch<'0'||ch>'9')&&ch!='-')
        ch=getchar();
    if(ch=='-'){
        flagg=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        xx=xx*10+ch-'0';
        ch=getchar();
    }
    return xx*flagg;
}
void pus(long long xx,long long flagg){
    if(xx<0){
        putchar('-');
        xx=-xx;
    }
    if(xx>=10)
        pus(xx/10,0);
    putchar(xx%10+'0');
    if(flagg==1)
        putchar(' ');
    if(flagg==2)
        putchar('\n');
    return;
}
long long n,m,i,j,xia[200005],zuo[200005],book[200005],book2[200005],sum1,sum2,now,flag;
long long x,y,z,top2,zhan1[200005],zhan2[200005],sum,ans,sum3[200005],jc[200005],ny[200005];
long long top=1,nex[400005],to[400005],fir[200005],p1,p2,p3;
long long top3,nex2[400005],to2[400005],fir2[200005];
long long maxx(long long u,long long v){
	return u>v?u:v;
}
long long ksm(long long u,long long v){
	long long o=1;
	while(v){
		if(v&1)
			o=o*u%mo;
		u=u*u%mo;
		v>>=1;	
	}
	return o;
}
void lj(int u,int v){
	top++;
	nex[top]=fir[u];
	fir[u]=top;
	to[top]=v;
}
void lj2(int u,int v){
	top3++;
	nex2[top3]=fir2[u];
	fir2[u]=top3;
	to2[top3]=v;
}
struct nyh{
	int x,y,id;
}o[200005];
bool cmp(nyh u,nyh v){
	return u.x<v.x||u.x==v.x&&u.y<v.y;
}
bool cmp2(nyh u,nyh v){
	return u.y<v.y||u.y==v.y&&u.x<v.x;
}
bool cmp3(nyh u,nyh v){
	return u.id<v.id;
}
void ss(int u,int v){
	book[v]=1;
	sum1++;
	for(int top1=fir[v];top1;top1=nex[top1]){
		sum2++;
		if(book[to[top1]]==0)
			ss(v,to[top1]);
	}
}
int ss2(int u,int v){
	if(flag==1)
		return 0;
	book[v]=2;
	for(int top1=fir[v];top1;top1=nex[top1])
		if(to[top1]!=u){
			if(book[to[top1]]==2){
				x=v;
				y=to[top1];
				z=top1/2;
				flag=1;
				return 0;
			}
			ss2(v,to[top1]);
			if(flag==1)
				return 0;
		}
}
void ss3(int u,int v,long long w){
	top2++;
	zhan1[top2]=v;
	zhan2[top2]=w;
	for(int top1=fir[v];top1;top1=nex[top1])
		if(to[top1]!=u&&top1/2!=z)
			ss3(v,to[top1],top1/2);
}
void ss4(int v){
	sum3[v]=1;
	book[v]=maxx(3,book[v]+1);
	for(int top1=fir2[v];top1;top1=nex2[top1]){
		if(book[to2[top1]]!=book[v])
			ss4(to2[top1]);		
		sum3[v]+=sum3[to2[top1]];
	}
	now=now*jc[sum3[v]-1]%mo*ny[sum3[v]]%mo;
}
int main(){
	n=read();m=n*2;
	jc[0]=1;
	for(i=1;i<=m;i++)
		jc[i]=jc[i-1]*i%mo;
	ny[m]=ksm(jc[m],mo-2);
	for(i=m-1;i>=0;i--)
		ny[i]=ny[i+1]*(i+1)%mo;
	for(i=1;i<=m;i++){
		o[i].x=read();
		o[i].y=read();
		o[i].id=i;
	}
	sort(o+1,o+m+1,cmp);
	for(i=2;i<=m;i++)
		if(o[i].x==o[i-1].x)
			xia[o[i].id]=o[i-1].id;
	sort(o+1,o+m+1,cmp2);
	for(i=2;i<=m;i++)
		if(o[i].y==o[i-1].y)
			zuo[o[i].id]=o[i-1].id;
	sort(o+1,o+m+1,cmp3);
	for(i=1;i<=m;i++){
		lj(o[i].x,o[i].y+n);
		lj(o[i].y+n,o[i].x);
	}
	ans=jc[m];
	for(i=1;i<=m;i++)
		if(book[i]==0){
			ss(i,i);
			if(sum1!=sum2/2){
				printf("0\n");
				return 0;
			}
			flag=0;
			ss2(i,i);
			sum=0;

			top2=0;
			ss3(x,x,z);
			now=jc[top2];
			for(j=1;j<=top2;j++)
				book2[zhan2[j]]=zhan1[j];
			for(j=1;j<=top2;j++){
				if(zhan1[j]>n){
					p1=zuo[zhan2[j]];
					while(p1!=0){
						lj2(zhan1[j],book2[p1]);
						p1=zuo[p1];
					}
				}
				else{
					p1=xia[zhan2[j]];
					while(p1!=0){
						lj2(zhan1[j],book2[p1]);
						p1=xia[p1];
					}
				}
			}
			for(j=1;j<=top2;j++)
				if(book[zhan1[j]]!=3)
					ss4(zhan1[j]);
			for(j=1;j<=top3;j++){
				nex2[j]=0;
				to2[j]=0;
			}
			fir2[0]=0;
			for(j=1;j<=top2;j++)
				fir2[zhan1[j]]=0;
			top3=0;
			sum=(sum+now)%mo;
			
			top2=0;
			ss3(y,y,z);
			now=jc[top2];
			for(j=1;j<=top2;j++)
				book2[zhan2[j]]=zhan1[j];
			for(j=1;j<=top2;j++){
				if(zhan1[j]>n){
					p1=zuo[zhan2[j]];
					while(p1!=0){
						lj2(zhan1[j],book2[p1]);
						p1=zuo[p1];
					}
				}
				else{
					p1=xia[zhan2[j]];
					while(p1!=0){
						lj2(zhan1[j],book2[p1]);
						p1=xia[p1];
					}
				}
			}
			for(j=1;j<=top2;j++)
				if(book[zhan1[j]]!=4)
					ss4(zhan1[j]);
			for(j=1;j<=top3;j++){
				nex2[j]=0;
				to2[j]=0;
			}
			fir2[0]=0;
			for(j=1;j<=top2;j++)
				fir2[zhan1[j]]=0;
			top3=0;
			sum=(sum+now)%mo;
			ans=ans*sum%mo*ny[top2]%mo;
		}
	pus(ans,2);
   	return 0;
}

Measured memory_bytes: 34816