// This amazing code is by Eric Sunli Chen.
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
template<typename T> void get_int(T &x)
{
	char t=getchar();
	bool neg=false;
	x=0;
	for(; (t>'9'||t<'0')&&t!='-'; t=getchar());
	if(t=='-')neg=true,t=getchar();
	for(; t<='9'&&t>='0'; t=getchar())x=x*10+t-'0';
	if(neg)x=-x;
}
template<typename T> void print_int(T x)
{
	if(x<0)putchar('-'),x=-x;
	short a[20]= {},sz=0;
	while(x>0)a[sz++]=x%10,x/=10;
	if(sz==0)putchar('0');
	for(int i=sz-1; i>=0; i--)putchar('0'+a[i]);
}
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define get1(a) get_int(a)
#define get2(a,b) get1(a),get1(b)
#define get3(a,b,c) get1(a),get2(b,c)
#define printendl(a) print_int(a),puts("")
typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int,int> pii;
const int inf=0x3f3f3f3f;
const LL Linf=1ll<<61;
const double pi=acos(-1.0);

const int mod=1e9+7;

int power(int x,int y)
{
	int ret=1;
	while(y)
	{
		if(y&1)ret=1ll*ret*x%mod;
		x=1ll*x*x%mod;
		y>>=1;
	}
	return ret;
}
int fac[200111],invf[200111];
int cal(int x,int y){return 1ll*fac[x+y]*invf[x]%mod*invf[y]%mod;}
pii comb(pii x,pii y){return mp(1ll*x.ff*y.ff%mod*cal(x.ss,y.ss)%mod,x.ss+y.ss);}

vector<int> g[200111],g2[200111];
int n,vs[200111],tot,cyc[200111],len,to[200111],dgr[200111];

bool incyc[200111],use[200111];
int stk[200111],top,cnt,dep[200111];

pii dfs2(int x)
{
	pii ret=mp(1,0);
	for(int i=0;i<(int)g2[x].size();i++)
		ret=comb(ret,dfs2(g2[x][i]));
	ret.ss++;return ret;
}
void dfs(int x,int f=0)
{
	stk[++top]=x;dep[x]=top;cnt+=2;
	vs[++tot]=x;
	for(int i=0;i<(int)g[x].size();i++)
	{
		cnt--;
		if(g[x][i]==f)continue;
		if(!dep[g[x][i]])
			dfs(g[x][i],x);
		else if(dep[g[x][i]]<dep[x]&&len==0)
		{
			for(int j=dep[g[x][i]];j<=top;j++){cyc[++len]=stk[j];incyc[stk[j]]=1;}
		}
	}
	top--;
}
void rdfs(int x,int f=0)
{
	to[x]=f;
	for(int i=0;i<(int)g[x].size();i++)
		if(g[x][i]!=f&&!incyc[g[x][i]])
			rdfs(g[x][i],x);
}

int calc()
{
	for(int i=1;i<=tot;i++){g2[vs[i]].clear();dgr[vs[i]]=0;}
	for(int i=1;i<=tot;i++)
	{
		for(int j=0;j<(int)g[vs[i]].size();j++)
		{
			if(to[vs[i]]>g[vs[i]][j])
			{
				g2[vs[i]].pb(g[vs[i]][j]);
				dgr[g[vs[i]][j]]++;
			}
		}
	}
	pii ret=mp(1,0);
	for(int i=1;i<=tot;i++)if(dgr[vs[i]]==0)ret=comb(ret,dfs2(vs[i]));
	return ret.ff;
}
int main()
{
	fac[0]=invf[0]=1;for(int i=1;i<200111;i++)fac[i]=1ll*fac[i-1]*i%mod;
	invf[200111-1]=power(fac[200111-1],mod-2);
	for(int i=200111-2;i>=0;i--)invf[i]=1ll*invf[i+1]*(i+1)%mod;
	
	get1(n);
	for(int i=1,x,y;i<=n+n;i++)
	{
		get2(x,y);
		g[x].pb(y+n);
		g[y+n].pb(x);
	}
	n<<=1;
	
	pii now=mp(1,0);
	for(int _=1;_<=n;_++)
	{
		if(dep[_])continue;
		tot=len=0;dfs(_);
		if(cnt)
		{
			puts("0");
			return 0;
		}
		
		for(int i=1;i<=len;i++)rdfs(cyc[i]);
		int sum=0;
		for(int i=1;i<=len;i++)to[cyc[i]]=cyc[i%len+1];
		sum+=calc();
		for(int i=1;i<=len;i++)to[cyc[i%len+1]]=cyc[i];
		sum+=calc();if(sum>=mod)sum-=mod;
		now=comb(now,mp(sum,tot));
	}
	
	printendl(now.ff);
	return 0;
}

Measured memory_bytes: 28032