#include<bits/stdc++.h>
using namespace std;

struct edge
{
	int s,t,w,n;
} e[1000000];

int n,h[1000000],fac[1000000],ifac[1000000],vis[1000000],f[1000000],siz[1000000],ecnt,cnt,S,p,ans,res;

int ksm(int a,int b)
{
	int x=a;
	a=1;
	while(b)
	{
		if(b&1)
			a=1ll*a*x%1000000007;
		x=1ll*x*x%1000000007,b>>=1;
	}
	return a;
}

int C(int n,int m)
{
	return 1ll*fac[n]*ifac[m]%1000000007*ifac[n-m]%1000000007;
}

void dfs(int x,int fa)
{
	vis[x]=1,cnt++;
	for(int i=h[x],y; y=e[i].t,i; ecnt++,i=e[i].n)
		if(!vis[y])
			f[y]=i,dfs(y,x);
		else if((y!=fa)&&(!S)) 
			S=i;
}

void dfs2(int x,int fe)
{
	siz[x]=0,f[x]=1;
	for(int i=h[x],y; y=e[i].t,i; i=e[i].n)
		if((i!=fe)&&((i^p)>1))
		{
			dfs2(y,i^1);
			if(e[i].w>e[fe].w)
				res=1ll*res*ifac[siz[y]]%1000000007*f[y]%1000000007;
			else
				siz[x]+=siz[y],f[x]=1ll*f[x]*f[y]%1000000007*C(siz[x],siz[y])%1000000007;
		}
	siz[x]++;
}

int main()
{
	scanf("%d",&n),fac[0]=1;
	for(int i=1; i<=2*n; i++)
		fac[i]=1ll*fac[i-1]*i%1000000007;
	ifac[2*n]=ksm(fac[2*n],1000000007-2);
	for(int i=2*n; i; i--)
		ifac[i-1]=1ll*ifac[i]*i%1000000007;
	for(int i=1,x,y,tot=1; i<=2*n; i++)
	{
		scanf("%d %d",&x,&y),y+=n;
		e[++tot]=(edge)
		{
			x,y,x+y,h[x]
		};
		h[x]=tot;
		e[++tot]=(edge)
		{
			y,x,x+y,h[y]
		};
		h[y]=tot;
	}
	ans=fac[2*n];
	for(int i=1,s; i<=2*n; i++)
		if(!vis[i])
		{
			ecnt=cnt=S=s=0,dfs(i,0);
			if(cnt!=ecnt/2)
			{
				puts("0");
				return 0;
			}
			p=S;
			for(int j=S; e[j].s!=e[S].t; j=f[e[j].s])
				p=(e[p].w<e[j].w?j:p);
			res=1;
			dfs2(e[p].s,p);
			s+=1ll*ifac[siz[e[p].s]]*f[e[p].s]%1000000007*res%1000000007;
			res=1;
			dfs2(e[p].t,p);
			s+=1ll*ifac[siz[e[p].t]]*f[e[p].t]%1000000007*res%1000000007;
			ans=1ll*ans*s%1000000007;
		}
	printf("%d",ans);
	return 0;
}
