#include<bits/stdc++.h>
#define mo 1000000007
#define ll long long
#define N 200005
using namespace std;
vector<int> e[N];
int vis[N],du[N],fa[N],sz[N];
int q[N],t,nq[N],n;
ll power(ll x,ll y){
	ll s=1;
	for (;y;y/=2,x=1ll*x*x%mo)
		if (y&1) s=1ll*s*x%mo;
	return s;
}
ll solve(int rt){
	t=1; q[1]=rt; vis[rt]=1;
	for (int h=1;h<=t;h++){
		int x=q[h]; du[x]=0;
		sz[x]=1; fa[x]=0;
		for (int i=0;i<e[x].size();i++){
			int to=e[x][i];
			if (!vis[to]){
				q[++t]=to;
				vis[to]=1;
			}
		}
	}
	int ecnt=0;
	for (int i=1;i<=t;i++)
		for (int j=0;j<e[q[i]].size();j++)
			du[e[q[i]][j]]++,ecnt++;
	if (ecnt/2!=t) return 0;
	int nt=0;
	for (int i=1;i<=t;i++)
		if (du[q[i]]==1) nq[++nt]=q[i];
	for (int h=1;h<=nt;h++){
		int x=nq[h];
		for (int i=0;i<e[x].size();i++){
			int to=e[x][i];
			if (--du[to]){
				fa[x]=to;
				if (du[to]==1)
					nq[++nt]=to;
				else rt=to;
			}
		}
	}
	for (int x=rt;;){
		for (int i=0;i<e[x].size();i++)
			if (du[e[x][i]]>1&&fa[e[x][i]]!=x){
				fa[x]=e[x][i]; x=e[x][i];
				break;
			}
		if (x==rt) break;
	}
	ll ans=1;
	for (int i=1;i<=nt;i++){
		int x=nq[i];
		ans=ans*sz[x]%mo;
		if (x<fa[fa[x]]) sz[fa[x]]+=sz[x];
	}
	for (int x=rt;;){
		if (x>fa[fa[x]]){
			rt=fa[x];
			break;
		}
		x=fa[x];
		if (x==rt) return 0;
	}
	for (int x=rt;;){
		ans=ans*sz[x]%mo;
		if (x<fa[fa[x]]) sz[fa[x]]+=sz[x];
		x=fa[x]; if (x==rt) break;
	}
	return power(ans,mo-2);
}
int main(){
	scanf("%d",&n);
	for (int i=1;i<=n*2;i++){
		int x,y;
		scanf("%d%d",&x,&y);
		e[x].push_back(y+n);
		e[y+n].push_back(x);
	}
	ll ans=1;
	for (int i=1;i<=n*2;i++)
		ans=ans*i%mo;
	for (int x=1;x<=n*2;x++)
		if (!vis[x]){
			ll s=solve(x);
			for (int i=1;i<=t;i++){
				int x=q[i]; vis[x]=0;
				reverse(e[x].begin(),e[x].end());
			}
			ans=ans*(s+solve(x))%mo;
		}
	printf("%lld",ans);
}