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++ (GCC 9.2.1) (judge verdict: Accepted)

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define isNum(a) (a >= '0' && a <= '9')
#define SP putchar(' ')
#define EL putchar('\n')
#define File(a) freopen(a ".in", "r", stdin), freopen(a ".out", "w", stdout)

template <class T> void read(T &);
template <class T> void write(const T &);

typedef long long ll;
typedef unsigned long long ull;
typedef const long long &cll;
typedef const int &ci;
typedef std::pair<int, int> pii;
const int iinf = 2147483647;
const ll llinf = 9223372036854775807ll;
using std::abs;
using std::max;
using std::min;

const int N = 200005;
const int M = 400005;
const int Mod = 1000000007;

int pow(int a, int b, int m);
void add(int u, int v);
void add2(int u, int v);
void dfs1(int u, int fa);
void dfs2(int u, int fa);
void dfs3(int u, int fa);
int solve();

int hed[N], nxt[M], to[M], id;
int hed2[N], nxt2[M], to2[M], id2;
int fac[N], ifac[N], inv[N];
int sta[N];
bool vis[N];
int deg[N];
int sv, se;
int nto[N];
int siz[N];
int X, Y;
int n;

int main() {
  read(n);
  for (int i = 1; i <= 2 * n; ++i) {
    int x, y;
    read(x), read(y);
    y += n;
    add(x, y), add(y, x);
  }
  n <<= 1;
  fac[0] = 1;
  for (int i = 1; i <= n; ++i) {
    fac[i] = fac[i - 1] * 1ll * i % Mod;
  }
  ifac[n] = pow(fac[n], Mod - 2, Mod);
  for (int i = n - 1; i >= 1; --i) {
    ifac[i] = ifac[i + 1] * 1ll * (i + 1) % Mod;
  }
  inv[1] = 1;
  for (int i = 2; i <= n; ++i) {
    inv[i] = (Mod - Mod / i) * 1ll * inv[Mod % i] % Mod;
  }
  int ans = fac[n];
  for (int i = 1; i <= n; ++i) {
    if (!vis[i]) {
      sv = se = 0;
      dfs1(i, 0);
      if ((sv << 1) != se) {
        puts("0");
        return 0;
      }
      ans = ans * 1ll * ifac[sv] % Mod;
      int now = solve();
      std::swap(X, Y);
      now = (now + solve()) % Mod;
      ans = ans * 1ll * now % Mod;
    }
  }
  write(ans), EL;
  return 0;
}

template <class T> inline void read(T &Re) {
  T k = 0;
  char ch = getchar();
  int flag = 1;
  while (!isNum(ch)) {
    if (ch == '-') {
      flag = -1;
    }
    ch = getchar();
  }
  while (isNum(ch)) {
    k = (k << 1) + (k << 3) + ch - '0';
    ch = getchar();
  }
  Re = flag * k;
}
template <class T> inline void write(const T &Wr) {
  if (Wr < 0) {
    putchar('-');
    write(-Wr);
  } else {
    if (Wr < 10) {
      putchar(Wr + '0');
    } else {
      write(Wr / 10);
      putchar((Wr % 10) + '0');
    }
  }
}

int pow(int a, int b, int m) {
  int ans = 1, now = a;
  while (b) {
    if (b & 1) {
      ans = ans * 1ll * now % m;
    }
    now = now * 1ll * now % m;
    b >>= 1;
  }
  return ans;
}
void add(int u, int v) {
  nxt[++id] = hed[u];
  hed[u] = id;
  to[id] = v;
}
void add2(int u, int v) {
  nxt2[++id2] = hed2[u];
  hed2[u] = id2;
  to2[id2] = v;
}
void dfs1(int u, int fa) {
  vis[u] = true;
  sta[++sv] = u;
  for (int i = hed[u]; i; i = nxt[i], ++se) {
    int v = to[i];
    if (!vis[v]) {
      dfs1(v, u);
    } else {
      if (v != fa) {
        X = u, Y = v;
      }
    }
  }
}
void dfs2(int u, int fa) {
  for (int i = hed[u]; i; i = nxt[i]) {
    int v = to[i];
    if (v != fa && v != X) {
      nto[v] = u;
      dfs2(v, u);
    }
  }
}
void dfs3(int u, int fa) {
  siz[u] = 1;
  for (int i = hed2[u]; i; i = nxt2[i]) {
    int v = to2[i];
    if (v != fa) {
      dfs3(v, u);
      siz[u] += siz[v];
    }
  }
}
int solve() {
  dfs2(X, Y);
  nto[X] = Y;
  for (int i = 1; i <= sv; ++i) {
    hed2[sta[i]] = deg[sta[i]] = 0;
  }
  for (int i = 1; i <= sv; ++i) {
    for (int j = hed[sta[i]]; j; j = nxt[j]) {
      int v = to[j];
      if (v < nto[sta[i]]) {
        add2(sta[i], v);
        ++deg[v];
      }
    }
  }
  for (int i = 1; i <= sv; ++i) {
    if (deg[sta[i]] == 0) {
      dfs3(sta[i], 0);
    }
  }
  int ans = fac[sv];
  for (int i = 1; i <= sv; ++i) {
    ans = ans * 1ll * inv[siz[sta[i]]] % Mod;
  }
  return ans;
}