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 Java8 (OpenJDK 1.8.0) (judge verdict: Time Limit Exceeded)

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.InputMismatchException;
import java.util.Scanner;

public class Main implements Runnable {
	static InputStream is;
	static PrintWriter out;
	static String INPUT = "";

	public static void main(String[] args) {
		new Thread(null, new Main(), "", Runtime.getRuntime().maxMemory()).start();
	}

	final long MOD = 1_000_000_000 + 7;
	final int N = 100000;
	long[] fac = new long[2 * N + 1];
	long[] invfac = new long[2 * N + 1];
	long[] inv = new long[2 * N + 1];
	{
		fac[0] = 1;
		invfac[0] = 1;
		inv[1] = 1;
		for (int i = 1; i < fac.length; ++i)
			fac[i] = fac[i - 1] * i % MOD;
		for (int i = 2; i < inv.length; ++i) {
			inv[i] = MOD - inv[(int) (MOD % i)] * (MOD / i) % MOD;
		}
		for (int i = 1; i < invfac.length; ++i) {
			invfac[i] = invfac[i - 1] * inv[i] % MOD;
		}
	}

	long c(int n, int k) {
		return fac[n] * invfac[k] % MOD * invfac[n - k] % MOD;
	}

	class DJSet {
		int[] upper;
		int[] e_sz;
		int n;

		public DJSet(int n_) {
			n = n_;
			upper = new int[n];
			e_sz = new int[n];
			Arrays.fill(upper, -1);
		}

		int root(int x) {
			return upper[x] < 0 ? x : (upper[x] = root(upper[x]));
		}

		boolean equiv(int x, int y) {
			return root(x) == root(y);
		}

		void setUnion(int x, int y) {
			x = root(x);
			y = root(y);
			if (x == y) {
				++e_sz[x];
				return;
			}
			if (upper[x] < upper[y]) {
				x ^= y;
				y ^= x;
				x ^= y;
			}
			upper[y] += upper[x];
			upper[x] = y;
			e_sz[y] += e_sz[x] + 1;
			e_sz[x] = -1;
		}

		int sz(int x) {
			return -upper[root(x)];
		}
	}

	void scc(ArrayList<Integer>[] g) {
		int n = g.length;
		col = new int[n];
		int[] ord = new int[n];
		int[] low = new int[n];
		col_sz = new int[n];
		Arrays.fill(col, -1);
		Arrays.fill(ord, -1);
		ArrayDeque<Integer> pend = new ArrayDeque<>();
		order = 0;
		color = 0;
		for (int i = 0; i < n; ++i) {
			if (col[i] == -1)
				scc_dfs(i, -1, g, ord, low, pend);
		}
		col_sz = Arrays.copyOf(col_sz, color);
	}

	void scc_dfs(int cur, int par, ArrayList<Integer>[] g, int[] ord, int[] low, ArrayDeque<Integer> pend) {
		pend.addLast(cur);
		ord[cur] = order++;
		low[cur] = ord[cur];
		for (int dst : g[cur]) {
			if (dst == par || col[dst] != -1)
				continue;
			if (ord[dst] != -1) {
				low[cur] = Math.min(low[cur], low[dst]);
			} else {
				scc_dfs(dst, cur, g, ord, low, pend);
				low[cur] = Math.min(low[cur], low[dst]);
			}
		}
		if (ord[cur] == low[cur]) {
			int v = -1;
			while (v != cur) {
				v = pend.pollLast();
				col[v] = color;
				col_sz[color]++;
			}
			++color;
		}
	}

	int[] col;
	int[] col_sz;
	int color;
	int order;

	long[] dfs2(int cur, ArrayList<Integer>[] g, boolean[] vis) {
		long pat = 1;
		int sz = 0;
		vis[cur] = true;
		for (int dst : g[cur]) {
			long[] q = dfs2(dst, g, vis);
			sz += q[1];
			pat = pat * q[0] % MOD * c(sz, (int) q[1]) % MOD;
		}
		sz += 1;
		return new long[] { pat, sz };
	}

	void dfs1(ArrayList<Integer>[] g, int cur, int par, ArrayList<Integer>[] forward, ArrayList<Integer>[] backward) {
		boolean update = false;
		for (int v : g[cur]) {
			if (v == par)
				continue;
			if (backward[v].size() == 0) {
				if (col_sz[col[cur]] > 1 && col_sz[col[v]] > 1) {
					if (update)
						continue;
					else
						update = true;
				}
				forward[v].add(cur);
				backward[cur].add(v);
				dfs1(g, v, cur, forward, backward);
			} else if (col_sz[col[cur]] > 1 && col_sz[col[v]] > 1 && !update) {
				forward[v].add(cur);
				backward[cur].add(v);
				update = true;
			}
		}
	}

	long f(ArrayList<Integer>[] g, ArrayList<Integer> v) {

		int n = g.length;
		ArrayList<Integer>[] g2 = new ArrayList[n];
		for (int i = 0; i < g2.length; ++i)
			g2[i] = new ArrayList<>();

		scc(g);

		long ret = 0;

		ArrayList<Integer>[] forward = new ArrayList[n];
		ArrayList<Integer>[] backward = new ArrayList[n];
		for (int i = 0; i < n; ++i) {
			forward[i] = new ArrayList<>();
			backward[i] = new ArrayList<>();
		}

		for (int i = 0; i < n; ++i) {
			if (col_sz[col[i]] > 1) {
				dfs1(g, i, -1, forward, backward);
				break;
			}
		}

		int[] frm = new int[n];
		for (int t = 0; t < 2; ++t) {
			for (int i = 0; i < g2.length; ++i)
				g2[i].clear();
			Arrays.fill(frm, -1);
			for (int i = 0; i < n; ++i) {
				for (int b : backward[i]) {
					for (int f : forward[i]) {
						if (v.get(b) < v.get(f)) {
							g2[i].add(b);
							frm[b] = i;
						}
					}
				}
			}
			int sz = 0;
			long ans = 1;
			boolean[] vis = new boolean[n];
			for (int i = 0; i < n; ++i) {
				if (vis[i])
					continue;
				int j = i;
				while (frm[j] != -1) {
					j = frm[j];
				}
				long[] q = dfs2(j, g2, vis);
				sz += q[1];
				ans = ans * q[0] % MOD * c((int) sz, (int) q[1]) % MOD;
			}
			ret = ret + ans;
			if (ret >= MOD)
				ret -= MOD;
			if (t == 0) {
				ArrayList<Integer>[] nf = new ArrayList[n];
				ArrayList<Integer>[] nb = new ArrayList[n];
				for (int i = 0; i < n; ++i) {
					nf[i] = new ArrayList<>();
					nb[i] = new ArrayList<>();
				}
				for (int i = 0; i < n; ++i) {
					for (int u : forward[i]) {
						if (col_sz[col[i]] == 1) {
							nf[i].add(u);
							nb[u].add(i);
						} else if (col_sz[col[i]] > 1 && col_sz[col[u]] > 1) {
							nf[u].add(i);
							nb[i].add(u);
						}
					}
				}
				backward = nb;
				forward = nf;
			}
		}
		return ret;

	}

	long solve(int n, int[] x, int[] y) {
		DJSet ds = new DJSet(2 * n);
		ArrayList<Integer>[] vv = new ArrayList[2 * n];
		ArrayList<Integer>[][] gg = new ArrayList[2 * n][];
		for (int i = 0; i < 2 * n; ++i) {
			vv[i] = new ArrayList<>();
		}
		for (int i = 0; i < 2 * n; ++i) {
			ds.setUnion(x[i], y[i] + n);
		}

		int[] id = new int[2 * n];
		for (int i = 0; i < 2 * n; ++i) {
			vv[ds.root(i)].add(i);
		}
		for (int i = 0; i < 2 * n; ++i) {
			Collections.sort(vv[i]);
			for (int v : vv[i])
				id[v] = Collections.binarySearch(vv[i], v);
			gg[i] = new ArrayList[vv[i].size()];
			for (int j = 0; j < gg[i].length; ++j)
				gg[i][j] = new ArrayList<>();
		}
		for (int i = 0; i < 2 * n; ++i) {
			int r = ds.root(x[i]);
			int u = id[x[i]];
			int v = id[y[i] + n];
			gg[r][u].add(v);
			gg[r][v].add(u);
		}
		long ans = 1;
		int tot = 0;
		for (int i = 0; i < gg.length; ++i) {
			if (gg[i].length == 0)
				continue;
			if (ds.sz(i) != ds.e_sz[ds.root(i)]) {
				return 0;
			}
			tot += gg[i].length;
			ans = ans * f(gg[i], vv[i]) % MOD * c(tot, gg[i].length) % MOD;
		}
		return ans;
	}

	public void run() {
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);
		// long t = System.currentTimeMillis();
		int n = ni();
		int[] x = new int[2 * n];
		int[] y = new int[2 * n];
		for (int i = 0; i < 2 * n; ++i) {
			x[i] = ni();
			y[i] = ni();
			--x[i];
			--y[i];
		}
		out.println(solve(n, x, y));
		out.flush();
		// System.err.println(System.currentTimeMillis() - t);
	}

	static void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}

	private static boolean eof() {
		if (lenbuf == -1)
			return true;
		int lptr = ptrbuf;
		while (lptr < lenbuf)
			if (!isSpaceChar(inbuf[lptr++]))
				return false;

		try {
			is.mark(1000);
			while (true) {
				int b = is.read();
				if (b == -1) {
					is.reset();
					return true;
				} else if (!isSpaceChar(b)) {
					is.reset();
					return false;
				}
			}
		} catch (IOException e) {
			return true;
		}
	}

	private static byte[] inbuf = new byte[1024];
	static int lenbuf = 0, ptrbuf = 0;

	private static int readByte() {
		if (lenbuf == -1)
			throw new InputMismatchException();
		if (ptrbuf >= lenbuf) {
			ptrbuf = 0;
			try {
				lenbuf = is.read(inbuf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (lenbuf <= 0)
				return -1;
		}
		return inbuf[ptrbuf++];
	}

	private static boolean isSpaceChar(int c) {
		return !(c >= 33 && c <= 126);
	}

	// private static boolean isSpaceChar(int c) { return !(c >= 32 && c <=
	// 126); }
	private static int skip() {
		int b;
		while ((b = readByte()) != -1 && isSpaceChar(b))
			;
		return b;
	}

	private static double nd() {
		return Double.parseDouble(ns());
	}

	private static char nc() {
		return (char) skip();
	}

	private static String ns() {
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while (!(isSpaceChar(b))) {
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}

	private static char[] ns(int n) {
		char[] buf = new char[n];
		int b = skip(), p = 0;
		while (p < n && !(isSpaceChar(b))) {
			buf[p++] = (char) b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}

	private static char[][] nm(int n, int m) {
		char[][] map = new char[n][];
		for (int i = 0; i < n; i++)
			map[i] = ns(m);
		return map;
	}

	private static int[] na(int n) {
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
			a[i] = ni();
		return a;
	}

	private static int ni() {
		int num = 0, b;
		boolean minus = false;
		while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
			;
		if (b == '-') {
			minus = true;
			b = readByte();
		}

		while (true) {
			if (b >= '0' && b <= '9') {
				num = num * 10 + (b - '0');
			} else {
				return minus ? -num : num;
			}
			b = readByte();
		}
	}

	private static long nl() {
		long num = 0;
		int b;
		boolean minus = false;
		while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
			;
		if (b == '-') {
			minus = true;
			b = readByte();
		}

		while (true) {
			if (b >= '0' && b <= '9') {
				num = num * 10 + (b - '0');
			} else {
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
}