    #include<cstdio>
    const int MaxN=200010,mod=1000000007;
    int N;
    long long fac[MaxN],ifac[MaxN];
    long long inv(int a,int p=mod){return a==1?1:(1+p*(a-inv(p%a,a)))/a%p;}
    struct edge{int to;edge*next;}E[MaxN*2],*ne=E,*fir[MaxN];
    void link(int u,int v){*ne=(edge){v,fir[u]};fir[u]=ne++;}
    int Q[MaxN],deg[MaxN],S[MaxN],fa[MaxN],siz[MaxN],C[MaxN];
    bool vis[MaxN];
    int dfs(int i){
    	siz[i]=1;
    	int s=1;
    	for(edge*e=fir[i];e;e=e->next)
    		if(fa[e->to]==i&&e->to<fa[i])s=1ll*s*dfs(e->to)%mod,siz[i]+=siz[e->to];
    	return 1ll*s*ifac[siz[i]]%mod*fac[siz[i]-1]%mod;
    }
    int solve(int v){
    	int*h=Q,*t=Q+1,es=0;
    	vis[*Q=v]=1;
    	for(;h<t;es+=deg[*h++])
    		for(edge*e=fir[*h];e;e=e->next,deg[*h]++)
    			if(!vis[e->to])vis[*t++=e->to]=1;
    	if(es!=(t-Q)*2)return 0;
    	int*tp=S;
    	for(int*i=Q;i<t;i++)if(deg[*i]==1)*tp++=*i;
    	while(tp>S){
    		int x=*--tp;
    		for(edge*e=fir[x];e;e=e->next)
    			if(--deg[e->to])deg[fa[x]=e->to]==1?*tp++=e->to:1;
    	}
    	int len=0;
    	for(int*i=Q;i<t;i++)if(deg[*i]>1){
    		for(int j=*i;deg[j];){
    			deg[C[len++]=j]=0;
    			for(edge*e=fir[j];e;e=e->next)
    				if(deg[e->to]>1){j=e->to;break;}
    		}
    		break;
    	}
    	int sum=0;
    	for(int dir=0;dir<2;dir++){
    		int s=1;
    		for(int i=0;i<len;i++)fa[C[i]]=C[(dir?i+len-1:i+1)%len];
    		for(int*i=Q;i<t;i++)if(*i>fa[fa[*i]])s=1ll*s*dfs(*i)%mod;
    		(sum+=s)%=mod;
    	}
    	return sum;
    }
    int main(){
    	scanf("%d",&N);
    	for(int i=*fac=1;i<=N*2;i++)fac[i]=fac[i-1]*i%mod;
    	ifac[N*2]=inv(fac[N*2]);
    	for(int i=N*2;i;i--)ifac[i-1]=ifac[i]*i%mod;
    	for(int i=0,x,y;i<N*2;i++)scanf("%d%d",&x,&y),link(x,y+=N),link(y,x);
    	int s=fac[N*2];
    	for(int i=1;i<=N*2;i++)if(!vis[i])s=1ll*s*solve(i)%mod;
    	printf("%d\n",s);
    }