#include <iostream>
#include <vector>
#include <map>
#include <utility>
#include <algorithm>
#define llint long long
#define mod 1000000007

using namespace std;
typedef pair<llint, llint> P;

struct UnionFind{
	int size;
	vector<int> parent;
	
	UnionFind(){}
	UnionFind(int size){
		this->size = size;
		parent.resize(size+1);
		init();
	}
	void init(){
		for(int i = 0; i <= size; i++) parent[i] = i;
	}
	int root(int i){
		if(parent[i] == i) return i;
		return parent[i] = root(parent[i]);
	}
	bool same(int i, int j){
		return root(i) == root(j);
	}
	void unite(int i, int j){
		int root_i = root(i), root_j = root(j);
		if(root_i == root_j) return;
		parent[root_i] = root_j;
	}
};

llint fact[200005], fact_inv[200005];

llint modpow(llint a, llint n)
{
	if(n == 0) return 1;
	if(n % 2){
		return ((a%mod) * (modpow(a, n-1)%mod)) % mod;
	}
	else{
		return modpow((a*a)%mod, n/2) % mod;
	}
}

void make_fact()
{
	llint val = 1;
	fact[0] = 1;
	for(int i = 1; i < 200005; i++){
		val *= i;
		val %= mod;
		fact[i] = val;
	}
	for(int i = 0; i < 200005; i++){
		fact_inv[i] = modpow(fact[i], mod-2);
	}
}

llint comb(llint n, llint k)
{
	llint ret = 1;
	ret *= fact[n];
	ret *= fact_inv[k], ret %= mod;
	ret *= fact_inv[n-k], ret %= mod;
	return ret;
}

llint n;
llint x[200005], y[200005];
vector<llint> G[200005];
UnionFind uf(200005);
map<llint, vector<llint> > mp;
vector<llint> cmp[200005];

bool used[200005];
bool cycled[200005];
bool pointed[200005];
vector<llint> g[200005], g2[200005];

vector<llint> cycle;
int cycledfs(int v, int pre)
{
	if(used[v]) return v;
	used[v] = true;
	for(int i = 0; i < G[v].size(); i++){
		if(G[v][i] == pre) continue;
		int res = cycledfs(G[v][i], v);
		if(res){
			cycle.push_back(v);
			cycled[v] = true;
			if(res == v) return 0;
			return res;
		}
	}
	return 0;
}

void treedfs(int v, int pre)
{
	for(int i = 0; i < G[v].size(); i++){
		if(G[v][i] == pre) continue;
		if(cycled[G[v][i]]) continue;
		g[G[v][i]].push_back(v);
		treedfs(G[v][i], v);
	}
}

llint merge(llint a, llint b)
{
	return comb(a+b, b);
}

P cntdfs(llint v)
{
	llint len = 0, ret = 1;
	for(int i = 0; i < g2[v].size(); i++){
		P res = cntdfs(g2[v][i]);
		ret *= res.first, ret %= mod;
		ret *= merge(len, res.second), ret %= mod;
		len += res.second;
	}
	len++;
	return make_pair(ret, len);
}

llint calc2(llint c)
{
	for(int i = 0; i < cmp[c].size(); i++) g2[cmp[c][i]].clear();
	for(int i = 0; i < cmp[c].size(); i++){
		int v = cmp[c][i];
		if(g[g[v][0]][0] > v) g2[g[v][0]].push_back(v);
	}
	
	for(int i = 0; i < cmp[c].size(); i++) pointed[cmp[c][i]] = false;
	for(int i = 0; i < cmp[c].size(); i++){
		int v = cmp[c][i];
		for(int j = 0; j < g2[v].size(); j++){
			pointed[g2[v][j]] = true;
		}
	}
	
	llint len = 0, ret = 1;
	for(int i = 0; i < cmp[c].size(); i++){
		if(!pointed[cmp[c][i]]){
			P res = cntdfs(cmp[c][i]);
			ret *= res.first, ret %= mod;
			ret *= merge(len, res.second), ret %= mod;
			len += res.second;
		}
	}
	return ret;
}

llint calc(llint c)
{
	cycle.clear();
	cycledfs(cmp[c][0], -1);
	for(int i = 0; i < cycle.size(); i++) treedfs(cycle[i], -1);
	
	llint ret = 0;
	int C = cycle.size();
	for(int i = 0; i < C; i++) g[cycle[i]].push_back(cycle[(i+1)%C]);
	ret += calc2(c), ret %= mod;
	
	for(int i = 0; i < C; i++) g[cycle[i]].clear();
	for(int i = 0; i < C; i++) g[cycle[i]].push_back(cycle[(i-1+C)%C]);
	ret += calc2(c), ret %= mod;
	
	return ret;
}

int main(void)
{
	make_fact();
	
	cin >> n;
	for(int i = 1; i <= 2*n; i++) cin >> x[i] >> y[i];
	for(int i = 1; i <= 2*n; i++){
		G[x[i]].push_back(y[i]+n);
		G[y[i]+n].push_back(x[i]);
		uf.unite(x[i], y[i]+n);
	}
	
	for(int i = 1; i <= 2*n; i++) mp[uf.root(i)].push_back(i);
	llint id = 0;
	for(auto it = mp.begin(); it != mp.end(); it++){
		for(int i = 0; i < it->second.size(); i++){
			cmp[id].push_back(it->second[i]);
		}
		id++;
	}
	
	for(int i = 0; i < id; i++){
		llint e = 0;
		for(int j = 0; j < cmp[i].size(); j++) e += G[cmp[i][j]].size();
		if(e/2 != cmp[i].size()){
			cout << 0 << endl;
			return 0;
		}
	}
	
	llint ans = 1, len = 0;
	for(int i = 0; i < id; i++){
		llint tmp = calc(i);
		ans *= tmp, ans %= mod;
		ans *= merge(len, cmp[i].size()), ans %= mod;
		len += cmp[i].size();
	}
	cout << ans << endl;
	
	return 0;
}