#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 = 400005;
const int M = 800005;
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;
}


Measured memory_bytes: 19476