#include<bits/stdc++.h>
typedef long long i64;
const int N=500007,P=1e9+7;
int n;
struct edge{
	int to,nx,del;
}e[N];
int e0[N],ep=2,cn,cm,deg[N],fa[N],fr[N],ss[N],sp=0,col[N],ff[N];
i64 fac[N],fiv[N];
i64 pw(i64 a,i64 n){
	i64 v=1;
	for(;n;n>>=1,a=a*a%P)if(n&1)v=v*a%P;
	return v;
}
void ae(int a,int b){
	e[ep]=(edge){b,e0[a],0};e0[a]=ep++;++deg[b];
}
void del(int w){
	rec:
	deg[ss[++sp]=w]=0;
	for(int i=e0[w];i;i=e[i].nx)if(!e[i].del){
		e[i].del=e[i^1].del=1;
		int u=e[i].to;
		fa[w]=u;
		if(--deg[u]==1){w=u;goto rec;}
	}
}
struct num{
	i64 v;
	int sz;
	void add(const num&w){v+=w.v,sz=w.sz;}
	void ins(const num&w){v=v*w.v%P,sz+=w.sz;}
	void inc(){v=v*fac[sz]%P*fiv[sz+1]%P;++sz;}
	i64 val(){return v*fac[sz]%P;}
}v1[N],v2[N],v3[N],ans=(num){1,0};
void cal(){
	for(int i=1;i<=n*2;++i){
		ff[i]=fa[fa[i]]>i?fa[i]:0;
		v1[i]=v2[i]=(num){1,0};
		deg[i]=0;
	}
	for(int i=1;i<=n*2;++i)++deg[ff[i]];
	for(int i=1;i<=n*2;++i)if(!deg[i]){
		for(int w=i,f;deg[w]=-1,f=ff[w];w=f){
			v1[w].inc();
			v1[f].ins(v1[w]);
			if(--deg[f])break;
		}
	}
	for(int i=1;i<=n*2;++i)if(!ff[i]){
		int f=col[i];
		v1[i].inc();
		v2[f].ins(v1[i]);
	}
	for(int i=1;i<=n*2;++i)if(col[i]==i)v3[i].add(v2[i]);
}
int main(){
	scanf("%d",&n);
	for(int i=fac[0]=1;i<=n*2;++i)fac[i]=i*fac[i-1]%P;
	fiv[n*2]=pw(fac[n*2],P-2);
	for(int i=n*2;i;--i)fiv[i-1]=i*fiv[i]%P;
	for(int i=1,a,b;i<=n*2;++i){
		scanf("%d%d",&a,&b);
		b+=n;
		ae(a,b),ae(b,a);
	}
	for(int i=1;i<=n*2;++i)if(deg[i]==1)del(i);
	for(int i=1;i<=n*2;++i)if(deg[i]&&deg[i]!=2)return puts("0"),0;
	for(int i=1;i<=n*2;++i)if(deg[i]){
		int p=sp+1;
		del(i);
		ss[sp+1]=ss[p];
		for(int j=p;j<=sp;++j)fr[ss[j+1]]=ss[j];
		int w=ss[sp];
		col[w]=w;
	}
	for(int i=sp;i;--i){
		int w=ss[i];
		if(!col[w])col[w]=col[fa[w]];
	}
	cal();
	for(int i=1;i<=n*2;++i)if(fr[i])fa[i]=fr[i];
	cal();
	for(int i=1,tot=0;i<=n*2;++i)if(col[i]==i)ans.ins(v3[i]);
	printf("%lld\n",ans.val());
	return 0;
}