#include <bits/stdc++.h>
#define ad(x) (((x - 1) ^ 1) + 1)

typedef long long ll;
const int N = 200054, M = N * 2, mod = 1000000007;

int n, E = 0, chksum;
int inv[N];
int to[M], first[N], next[M], deg[N];
int p[N], Cycu, Cycv;
int cnt = 0, o[N], id[N];
bool used[N];

void init() {
	int i;
	for (inv[1] = 1, i = 2; i < N; ++i) inv[i] = ll(mod - mod / i) * inv[mod % i] % mod;
}

inline void addedge(int u, int v) {
	to[++E] = v, next[E] = first[u], first[u] = E, ++deg[u];
	to[++E] = u, next[E] = first[v], first[v] = E, ++deg[v];
}

inline void __builtin_deledge(int u, int v) {
	if (to[first[u]] == v) first[u] = next[first[u]];
	else for (int i = first[u]; i; i = next[i])
		if (to[next[i]] == v) {next[i] = next[next[i]]; return;}
}

inline void deledge(int u, int v) {__builtin_deledge(u, v), __builtin_deledge(v, u);}

void dfs(int x, int px = 0) {
	int i, y; used[x] = true, chksum += 2 - deg[x];
	o[++cnt] = x, id[x] = cnt;
	for (i = first[x]; i; i = next[i])
		if (!used[y = to[i]]) dfs(y, x);
		else if ((y = to[i]) != px) Cycu = x, Cycv = y;
}

void dfs2(int x) {
	int i, y;
	for (i = first[x]; i; i = next[i])
		if ((y = to[i]) != p[x]) p[y] = x, dfs2(y);
}

namespace Topo {
	int p[N], fc[N], nc[N], prod;

	inline void link(int x, int px) {
		p[x] = px, nc[x] = fc[px], fc[px] = x;
	}

	int dfs(int x) {
		int y, sz = 1;
		for (y = fc[x]; y; y = nc[y]) sz += dfs(y);
		return prod = (ll)prod * inv[sz] % mod, sz;
	}

	int main() {
		int i, _; prod = 1;
		for (_ = 1; _ <= cnt; ++_) i = o[_], p[i] = fc[i] = 0;
		for (_ = 1; _ <= cnt; ++_) if (i = o[_], ::p[::p[i]] > i) link(i, ::p[i]);
		for (_ = 1; _ <= cnt; ++_) if (!p[i = o[_]]) dfs(i);
		return prod;
	}
}

int main() {
	int i, u, v, ans = 1, cur; init();
	scanf("%d", &n);
	for (i = n *= 2; i > 1; --i) ans = (ll)ans * i % mod;
	for (i = 0; i < n; ++i) scanf("%d%d", &u, &v), addedge(u, v + n / 2);
	for (i = 1; i <= n; ++i) if (!used[i]) {
		if (cnt = 0, dfs(i), chksum) return putchar(48), putchar(10), 0;
		deledge(Cycu, Cycv);
		p[Cycu] = Cycv, dfs2(Cycu), cur = Topo::main();
		p[Cycv] = Cycu, dfs2(Cycv), cur += Topo::main();
		ans = (ll)ans * cur % mod;
	}
	printf("%d\n", ans);
	return 0;
}