Problem:
Score : 1200 points

Problem Statement

There are 2N balls in the xy-plane. The coordinates of the i-th of them is (x_i, y_i).
Here, x_i and y_i are integers between 1 and N (inclusive) for all i, and no two balls occupy the same coordinates.

In order to collect these balls, Snuke prepared 2N robots, N of type A and N of type B.
Then, he placed the type-A robots at coordinates (1, 0), (2, 0), ..., (N, 0), and the type-B robots at coordinates (0, 1), (0, 2), ..., (0, N), one at each position.

When activated, each type of robot will operate as follows.

When a type-A robot is activated at coordinates (a, 0), it will move to the position of the ball with the lowest y-coordinate among the balls on the line x = a, collect the ball and deactivate itself. If there is no such ball, it will just deactivate itself without doing anything.

When a type-B robot is activated at coordinates (0, b), it will move to the position of the ball with the lowest x-coordinate among the balls on the line y = b, collect the ball and deactivate itself. If there is no such ball, it will just deactivate itself without doing anything.

Once deactivated, a robot cannot be activated again. Also, while a robot is operating, no new robot can be activated until the operating robot is deactivated.

When Snuke was about to activate a robot, he noticed that he may fail to collect all the balls, depending on the order of activating the robots.

Among the (2N)! possible orders of activating the robots, find the number of the ones such that all the balls can be collected, modulo 1 000 000 007.

Constraints

2 \leq N \leq 10^5

1 \leq x_i \leq N

1 \leq y_i \leq N

If i ≠ j, either x_i ≠ x_j or y_i ≠ y_j.

Inputs

Input is given from Standard Input in the following format:

N
x_1 y_1
...
x_{2N} y_{2N}

Outputs

Print the number of the orders of activating the robots such that all the balls can be collected, modulo 1 000 000 007.

Sample Input 1

2
1 1
1 2
2 1
2 2

Sample Output 1

8

We will refer to the robots placed at (1, 0) and (2, 0) as A1 and A2, respectively, and the robots placed at (0, 1) and (0, 2) as B1 and B2, respectively.
There are eight orders of activation that satisfy the condition, as follows:

A1, B1, A2, B2

A1, B1, B2, A2

A1, B2, B1, A2

A2, B1, A1, B2

B1, A1, B2, A2

B1, A1, A2, B2

B1, A2, A1, B2

B2, A1, B1, A2

Thus, the output should be 8.

Sample Input 2

4
3 2
1 2
4 1
4 2
2 2
4 4
2 1
1 3

Sample Output 2

7392

Sample Input 3

4
1 1
2 2
3 3
4 4
1 2
2 1
3 4
4 3

Sample Output 3

4480

Sample Input 4

8
6 2
5 1
6 8
7 8
6 5
5 7
4 3
1 4
7 6
8 3
2 8
3 6
3 2
8 5
1 5
5 8

Sample Output 4

82060779

Sample Input 5

3
1 1
1 2
1 3
2 1
2 2
2 3

Sample Output 5

0

When there is no order of activation that satisfies the condition, the output should be 0.

Solution in C++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define MOD 1000000007
#define ADD(X,Y) ((X) = ((X) + (Y)%MOD) % MOD)
typedef long long i64; typedef vector<int> ivec; typedef vector<string> svec;

i64 modpow(i64 a, int p = MOD - 2)
{
	if (p == 0) return 1;
	i64 tmp = modpow(a, p / 2);
	tmp = tmp * tmp % MOD;
	if (p % 2 == 1) tmp = tmp * a % MOD;
	return tmp;
}

int N, X[101010], Y[101010];
i64 fact[202020], facti[202020];

vector<int> ax[101010], ay[101010];

int uf[202020];

int root(int p)
{
	return uf[p] < 0 ? p : (uf[p] = root(uf[p]));
}
bool join(int p, int q)
{
	p = root(p);
	q = root(q);
	if (p == q) return false;
	uf[p] += uf[q];
	uf[q] = p;
	return true;
}

vector<int> grp[202020];
int dir[202020]; // 0: down, 1: left

set<int> sx[101010], sy[101010];
vector<int> depends[202020];
int depender[202020];

void add_dependency(int p, int q)
{
	// p depends on q
	depends[p].push_back(q);
	depender[q] = p;
}

pair<int, i64> solve_dependency(int p)
{
	int sz = 0;
	i64 pat = 1;

	for (int q : depends[p]) {
		//printf("%d -> %d\n", p, q);
		auto sub = solve_dependency(q);
		pat = pat * sub.second % MOD;
		pat = pat * facti[sub.first] % MOD;
		sz += sub.first;
	}
	pat = pat * fact[sz] % MOD;

	return{ sz + 1, pat };
}

i64 solve(int g)
{
	set<int> xs, ys;
	for (int c : grp[g]) {
		xs.insert(X[c]);
		ys.insert(Y[c]);
	}
	if (xs.size() + ys.size() != grp[g].size()) {
		return 0;
	}

	i64 ret = 0;
	for (int t = 0; t < 2; ++t) {
		for (int c : grp[g]) {
			depends[c].clear();
			depender[c] = -1;
			dir[c] = -1;
		}
		for (int x : xs) sx[x].clear();
		for (int y : ys) sy[y].clear();

		for (int c : grp[g]) {
			sx[X[c]].insert(c);
			sy[Y[c]].insert(c);
		}
		queue<pair<int, int>> Q;
		for (int x : xs) {
			if (sx[x].size() == 0) {
				return 0;
			}
			if (sx[x].size() == 1) {
				Q.push({ 0, *sx[x].begin() });
			}
		}
		for (int y : ys) {
			if (sy[y].size() == 0) {
				return 0;
			}
			if (sy[y].size() == 1) {
				Q.push({ 1, *sy[y].begin() });
			}
		}
		int rem = grp[g].size();

		while (rem > 0) {
			if (Q.empty()) {
				int tg = 0;
				for (int x : xs) {
					if (sx[x].size() > 0) {
						Q.push({ t, *sx[x].begin() });
						//printf("force %d %d\n", *sx[x].begin(), t);
						break;
					}
				}
				continue;
			}

			auto a = Q.front(); Q.pop();
			--rem;

			if (a.first == 0) {
				int p = a.second; // *sx[a.second].begin();
				//printf("decide %d %d\n", p, 0);
				dir[p] = 0;
				sx[X[p]].erase(p);
				sy[Y[p]].erase(p);
				//if (sy[Y[p]].size() == 0) return 0;
				if (sy[Y[p]].size() == 1) {
					//printf("%d\n", *sy[Y[p]].begin());
					Q.push({ 1, *sy[Y[p]].begin() });
				}
			} else {
				int p = a.second; // *sy[a.second].begin();
				//printf("decide %d %d\n", p, 1);
				dir[p] = 1;
				sx[X[p]].erase(p);
				sy[Y[p]].erase(p);
				//if (sx[X[p]].size() == 0) return 0;
				if (sx[X[p]].size() == 1) {
					Q.push({ 0, *sx[X[p]].begin() });
				}
			}
		}

		for (int c : grp[g]) {
			int x = X[c], y = Y[c];
		//	printf("%d %d: %d\n", x, y, dir[c]);
			if (dir[c] == 0) {
				for (int d : ax[x]) {
					if (Y[d] < y) {
						add_dependency(c, d);
					}
				}
			} else if (dir[c] == 1) {
				for (int d : ay[y]) {
					if (X[d] < x) {
						add_dependency(c, d);
					}
				}
			} else {
				puts("><");
			}
		}

		i64 pat = fact[grp[g].size()];
		for (int c : grp[g]) {
			if (depender[c] == -1) {
				pair<int, i64> sz_pat = solve_dependency(c);
				pat = pat * facti[sz_pat.first] % MOD * sz_pat.second % MOD;
			}
		}
		ADD(ret, pat);
	}

	return ret;
}

int main()
{
	scanf("%d", &N);
	for (int i = 0; i < 2 * N; ++i) {
		int x, y;
		scanf("%d%d", &x, &y);
		--x; --y;
		X[i] = x;
		Y[i] = y;
		ax[x].push_back(i);
		ay[y].push_back(i);
	}
	fact[0] = 1;
	for (int i = 1; i <= 2 * N; ++i) fact[i] = fact[i - 1] * i % MOD;
	for (int i = 0; i <= 2 * N; ++i) facti[i] = modpow(fact[i]);

	for (int i = 0; i < 2 * N; ++i) uf[i] = -1;
	for (int i = 0; i < N; ++i) {
		for (int j = 1; j < ax[i].size(); ++j) {
			join(ax[i][j - 1], ax[i][j]);
		}
		for (int j = 1; j < ay[i].size(); ++j) {
			join(ay[i][j - 1], ay[i][j]);
		}
	}
	for (int i = 0; i < 2 * N; ++i) {
		grp[root(i)].push_back(i);
	}

	i64 ret = fact[2 * N];
	for (int i = 0; i < 2 * N; ++i) {
		if (grp[i].size() > 0) {
			ret = ret * facti[grp[i].size()] % MOD * solve(i) % MOD;
			if (ret == 0) break;
		}
	}


	printf("%lld\n", ret);
	return 0;
}