#include<bits/stdc++.h>
#define mo 1000000007
#define next nxt
using namespace std;
int fac[400010],nifac[400010],e[400010];
int first[400010],next[400010],des[400010],w[400010];
int size[400010],ans,f[400010],vn,en,ed;
bool vis[400010];
int tt,n;
int C(int n,int m){return (long long)fac[n]*nifac[m]%mo*nifac[n-m]%mo;}
int getmi(int a,int x)
{
	int ans=1;
	while (x)
	{
		if (x&1) ans=(long long)ans*a%mo;
		a=(long long)a*a%mo;
		x>>=1;
	}
	return ans;
}
void dfs(int s,int pre)
{
	vis[s]=true;vn++;
	for (int k=first[s];k;k=next[k])
	{
		en++;
		if (!vis[des[k]]) {e[des[k]]=k^1;dfs(des[k],s);}
		else if (des[k]!=pre) ed=k;
	}
}
void dp(int s,int e)
{
	f[s]=1;size[s]=0;
	for (int k=first[s];k;k=next[k]) if (k/2!=ed/2&&((k^1)!=e))
	{
		dp(des[k],k);
		if (w[k]>w[e]) ans=(long long)ans*nifac[size[des[k]]]%mo*f[des[k]]%mo;
		else
		{
			size[s]+=size[des[k]];
			f[s]=(long long)f[s]*f[des[k]]%mo*C(size[s],size[des[k]])%mo;
		}
	}
	size[s]++;
}
int main()
{
	tt=1;scanf("%d",&n);n<<=1;
	fac[0]=nifac[0]=1;
	for (int i=1;i<=n;i++) {fac[i]=(long long)fac[i-1]*i%mo;nifac[i]=getmi(fac[i],mo-2);}
	for (int i=1;i<=n;i++)
	{
		int u,v;scanf("%d%d",&u,&v);v+=n/2;
		des[++tt]=v;next[tt]=first[u];first[u]=tt;
		des[++tt]=u;next[tt]=first[v];first[v]=tt;
		w[tt]=w[tt^1]=u+v;
	}
 
	int all=fac[n];
	for (int s=1;s<=n;s++) if (!vis[s])
	{
		vn=en=ed=0;dfs(s,0);en>>=1;
		if (vn!=en) {puts("0");return 0;}
		int u=des[ed],v=des[ed^1];
		for (;u!=v;u=des[e[u]]) if (w[e[u]]>w[ed]) ed=e[u];
		ans=1;dp(des[ed],ed);
		int p1=(long long)ans*nifac[size[des[ed]]]%mo*f[des[ed]]%mo;
		ans=1;dp(des[ed^1],ed);
		int p2=(long long)ans*nifac[size[des[ed^1]]]%mo*f[des[ed^1]]%mo;
		all=(long long)all*(p1+p2)%mo;
	}
	printf("%d\n",all);
}