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 <iostream>
#include <map>
#include <cassert>
#define travel(x, i) for (int i = fir[x]; i; i = e[i].nxt)
#define Travel(x, i) for (int i = Fir[x]; i; i = E[i].nxt)
using namespace std;

typedef long long LL;
const int N = 4e5 + 5;
const LL mod = 1e9 + 7;

template <typename T> inline void Read(T &x) {
  char ch; x = 0;
  while (!isdigit(ch = getchar()));
  do {x = (x << 1) + (x << 3) + ch - '0';} while (isdigit(ch = getchar()));
}

namespace MathCal {
  LL fac[N], inv[N];
  inline LL C(int x, int y) {
    if (x < y) return 0;
    return fac[x] * inv[y] % mod * inv[x - y] % mod;
  }
  inline LL pow(LL x, LL exp) {
    LL ret = 1;
    for (; exp; exp >>= 1, x = x * x % mod)
      if (exp & 1) ret = ret * x % mod;
    return ret;
  }
  inline void pre() {
    for (int i = fac[0] = 1; i < N; i ++) fac[i] = fac[i - 1] * i % mod;
    inv[N - 1] = pow(fac[N - 1], mod - 2);
    for (int i = N - 1; i >= 1; i --) inv[i - 1] = inv[i] * i % mod;
  }
}
using namespace MathCal;

struct edge {
  int nxt, to, id;
} e[N << 1];
int fir[N], cnt = 0, lst[N], deg[N], size[N], rt;

struct Edge {
  int nxt, to;
} E[N << 1];
int Fir[N], Cnt = 0;

bool vis[N], cycle[N], found = 0;
int n, x[N], y[N], con[N];
map <int, int> X[N], Y[N];
int vertex[N], top;

inline void add(int x, int y, int id) {
  e[++ cnt] = (edge){fir[x], y, id};
  fir[x] = cnt;
}

inline void Add(int x, int y) {
  //cerr << x << " -> " << y << endl;
  E[++ Cnt] = (Edge){Fir[x], y};
  Fir[x] = Cnt;
}

inline void dfs(int x) {
  vis[x] = 1;
  vertex[++ top] = x;
  travel(x, i) {
    if (e[i].to == lst[x]) continue;
    if (!found && vis[e[i].to]) {
      found = 1;
      rt = x;
      for (int p = x; p != e[i].to; p = lst[p]) cycle[p] = 1;
      cycle[e[i].to] = 1;
      break;
    }
    else if (!vis[e[i].to]) {
      lst[e[i].to] = x;
      dfs(e[i].to);
    }
  }
}

inline void Assign(int x, int pa) {
  travel(x, i)
    if (!cycle[e[i].to] && e[i].to != pa) {
      con[e[i].id] = e[i].to;
      Assign(e[i].to, x);
    }
}

inline void Assign_Cycle(int x, int pre, bool pattern) {
  travel(x, i)
    if (cycle[e[i].to] && pre != e[i].to) {
      if (pattern) con[e[i].id] = x;
      else con[e[i].id] = e[i].to;
      if (x != rt || !pre) Assign_Cycle(e[i].to, x, pattern);
      break;
    }
}

inline void Build() {
  for (int i = 1; i <= top; i ++)
    Fir[vertex[i]] = deg[vertex[i]] = 0;
  Cnt = 0;
  map <int, int> :: iterator it;
  for (int u = top, i; u; u --) {
    i = vertex[u];
    if (con[i] <= n) {
      it = X[x[i]].find(y[i]);
      while (it != X[x[i]].begin()) {
	it --;
	deg[it -> second] ++;
	Add(i, it -> second);
      }
    }
    else {
      it = Y[y[i]].find(x[i]);
      while (it != Y[y[i]].begin()) {
	it --;
	deg[it -> second] ++;
	Add(i, it -> second);
      }
    }
  }
}

inline void Get_Size(int x, int pa) {
  size[x] = 1;
  Travel(x, i) {
    Get_Size(E[i].to, x);
    size[x] += size[E[i].to];
  }
}

inline LL Calc(int x, int pa) {
  int p = size[x] - 1;
  LL res = 1;
  Travel(x, i) {
    res = res * Calc(E[i].to, x) % mod * C(p, size[E[i].to]) % mod;
    p -= size[E[i].to];
  }
  assert(!p);
  return res;
}

inline LL Solve() {
  LL res = 1;
  int tot = top;
  Build();
  for (int u = top, i = vertex[u]; u; u --, i = vertex[u])
    if (!deg[i]) {
      Get_Size(i, 0);
      res = res * Calc(i, 0) % mod * C(tot, size[i]) % mod;
      tot -= size[i];
    }
  assert(!tot);
  return res;
}

int main() {
  pre();
  Read(n);
  for (int i = 1; i <= (n << 1); i ++) {
    Read(x[i]); Read(y[i]);
    add(x[i], y[i] + n, i);
    add(y[i] + n, x[i], i);
    X[x[i]][y[i]] = i;
    Y[y[i]][x[i]] = i;
  }
  for (int i = (n << 1); i; i --)
    if (!fir[i]) return !puts("0");
  LL ans = 1, tmp;
  int p = n << 1;
  for (int k = 1; k <= (n << 1); k ++)
    if (!vis[k]) {
      found = 0;
      top = 0;
      dfs(k);
      for (int i = top; i; i --)
	if (cycle[vertex[i]]) Assign(vertex[i], 0);
      Assign_Cycle(rt, 0, 0);
      tmp = Solve();
      if (ans >= mod) ans -= mod;
      Assign_Cycle(rt, 0, 1);
      tmp += Solve();
      if (tmp >= mod) tmp -= mod;
      tmp = tmp * C(p, top) % mod;
      ans = ans * tmp % mod;
      p -= top;
    }
  assert(!p);
  printf("%lld\n", ans);
  return 0;
}