#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD=1e9+7;
ll powmod(ll a, ll k){
    ll ap=a, ans=1;
    while(k){
        if(k&1){
            ans*=ap;
            ans%=MOD;
        }
        ap=ap*ap;
        ap%=MOD;
        k>>=1;
    }
    return ans;
}
ll inv(ll a){
    return powmod(a, MOD-2);
}
ll f[200020], invf[200020];
void fac(int n){
    f[0]=1;
    for(ll i=1; i<=n; i++) f[i]=f[i-1]*i%MOD;
    invf[n]=inv(f[n]);
    for(ll i=n-1; i>=0; i--) invf[i]=invf[i+1]*(i+1)%MOD;
}
ll comb(int x, int y){
    if(!(0<=y && y<=x)) return 0;
    return f[x]*invf[y]%MOD*invf[x-y]%MOD;
}
int n;
int x[200020], y[200020];
vector<P> g[200020];
bool nuo[200020];
vector<int> v, ve;
void dfs0(int a){
	nuo[a]=1;
	v.push_back(a);
	for(auto p:g[a]){
		int b=p.first;
		ve.push_back(p.second);
		if(!nuo[b]){
			dfs0(b);
		}
	}
}
vector<P> gr[2][200020];
vector<int> gd[2][200020];
P g0[2][200020];
bool used[200020];
int d[200020];
ll dp[2][200020];
int sz[2][200020];
void dfs(int t, int a){
	dp[t][a]=1;
	for(auto b:gd[t][a]){
		dfs(t, b);
		sz[t][a]+=sz[t][b];
		(dp[t][a]*=invf[sz[t][b]])%=MOD;
		(dp[t][a]*=dp[t][b])%=MOD;
	}
	(dp[t][a]*=f[sz[t][a]])%=MOD;
	sz[t][a]++;
}
bool r[2][200020];
ll solve(){
	queue<int> que;
	for(auto a:v){
		if(d[a]==1) que.push(a);
	}
	while(!que.empty()){
		int a=que.front(); que.pop();
		for(auto p:g[a]){
			if(used[p.second]) continue;
			int b=p.first;
			used[p.second]=1;
			d[b]--;
			int i=p.second;
			g0[0][a]=g0[1][a]=P(b, i);
			gr[0][b].push_back({a, i});
			gr[1][b].push_back({a, i});
			if(d[b]==1) que.push(b);
		}
	}
	int now, pr=-1, cyc=0;
	for(auto i:ve){
		if(!used[i]){
			now=x[i];
			cyc++;
		}
	}
	for(int j=0; j<cyc; j++){
		for(auto p:g[now]){
			if(used[p.second] || p.first==pr) continue;
			pr=now;
			now=p.first;
			int i=p.second;
			g0[0][pr]=P(now, i);
			gr[0][now].push_back({pr, i});
			g0[1][now]=P(pr, i);
			gr[1][pr].push_back({now, i});
			break;
		}
	}
	ll ans=0;
	for(int t=0; t<2; t++){
		ll ans1=f[v.size()];
		for(auto a:v){
			int b=g0[t][a].first, i=g0[t][a].second;
			for(auto p:gr[t][a]){
				if(p.first<b){
					gd[t][i].push_back(p.second);
					r[t][p.second]=1;
				}
			}
		}
		for(auto i:ve){
			if(!r[t][i]){
				dfs(t, i);
				(ans1*=dp[t][i])%=MOD;
				(ans1*=invf[sz[t][i]])%=MOD;
			}
		}
		(ans+=ans1)%=MOD;
	}
	return ans;
}
int main()
{
	cin>>n;
	for(int i=0; i<2*n; i++){
		cin>>x[i]>>y[i]; x[i]--; y[i]--;
		y[i]+=n;
		d[x[i]]++; d[y[i]]++;
		g[x[i]].push_back({y[i], i});
		g[y[i]].push_back({x[i], i});
	}
	fac(2*n);
	ll ans=f[2*n];
	for(int i=0; i<2*n; i++){
		if(!nuo[i]){
			v.clear();
			ve.clear();
			dfs0(i);
			sort(ve.begin(), ve.end());
			ve.erase(unique(ve.begin(), ve.end()), ve.end());
			if(ve.size()!=v.size()){
				cout<<0<<endl;
				return 0;
			}
			(ans*=invf[v.size()])%=MOD;
			(ans*=solve())%=MOD;
		}
	}
	cout<<ans<<endl;
	return 0;
}

Measured memory_bytes: 65256