#include <cstdio>
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <map>

#define MAX_INT 2147483647
#define MOD 1000000007L
#define PI 3.141592653589793238462643383279502884L

#define long long long int

using std::vector;
using std::map;
using std::string;
using std::pair;
using std::cin;
using std::cout;
using std::cerr;

// @author: pashka

int n;
vector<vector<int>> g;
vector<vector<int>> gg;
vector<int> z;

int nn = 0;
int mm = 0;
vector<int> comp;
vector<int> st;
vector<int> cycle;
vector<int> des;
vector<long> dp;

void dfs(int x, int p) {
    if (z[x] == 2) return;
    if (z[x] == 1) {
        if (cycle.size() == 0) {
            int i = st.size() - 1;
            while (st[i] != x) {
                cycle.push_back(st[i]);
                i--;
            }
            cycle.push_back(x);
        }
        return;
    }
    st.push_back(x);
    comp.push_back(x);
    z[x] = 1;
    nn++;
    for (int y : g[x]) {
        mm++;
        if (y != p) {
            dfs(y, x);
        }
    }
    z[x] = 2;
    st.pop_back();
}

void dfs2(int x) {
    z[x] = 3;
    for (int y : g[x]) {
        if (z[y] != 3) {
            des[y] = x;
            dfs2(y);
        }
    }
}

vector<long> fact;
vector<long> inv_fact;

long mult(long a, long b) {
    return (a * b) % MOD;
}

long calc_c(int n, int k) {
    return mult(mult(fact[n], inv_fact[k]), inv_fact[n - k]);
}

long pow(long a, int pow) {
    long res = 1;
    while (pow > 0) {
        if (pow & 1) {
            res = mult(res, a);
        }
        a = mult(a, a);
        pow /= 2;
    }
    return res;
}

int dfs3(int x, int p) {
    int s = 0;
    dp[x] = 1;
    for (int y : gg[x]) {
        if (y != p) {
            int ss = dfs3(y, x);
            s += ss;
            dp[x] = mult(dp[x], dp[y]);
            dp[x] = mult(dp[x], calc_c(s, ss));
        }
    }
    return s + 1;
}

int main() {
    std::ios::sync_with_stdio(false);

    cin >> n;
    n *= 2;

    fact.resize(n + 1);
    inv_fact.resize(n + 1);
    fact[0] = inv_fact[0] = 1;
    for (int i = 1; i <= n; i++) {
        fact[i] = mult(fact[i - 1], i);
        inv_fact[i] = pow(fact[i], MOD - 2);
//        cout << fact[i] << " " << inv_fact[i] << "\n";
    }

    g.resize(n);
    gg.resize(n);
    for (int i = 0; i < n; i++) {
        int x, y;
        cin >> x >> y;
        x--;
        y--;
        g[x].push_back(y + n / 2);
        g[y + n / 2].push_back(x);
    }

    long res = 1;
    des.resize(n);
    z.resize(n);
    vector<int> deg(n);
    dp.resize(n);
    int q = 0;
    for (int i = 0; i < n; i++) {
        if (z[i]) continue;
        cycle.resize(0);
        comp.resize(0);
        nn = 0;
        mm = 0;
        dfs(i, -1);
//        cerr << nn << " " << mm << "\n";
        mm /= 2;
        if (mm != nn) {
            cout << "0\n";
            return 0;
        }
//        for (auto item : comp) {
//            cerr << item << " ";
//        }
//        cerr << "\n";
//        for (auto item : cycle) {
//            cerr << item << " ";
//        }
//        cerr << " cc\n";

        long ss = 0;

        for (int t = 0; t < 2; t++) {
            long pp = 1;
            for (int i = 0; i < cycle.size(); i++) {
                des[cycle[i]] = cycle[(i + 1) % cycle.size()];
                z[cycle[i]] = 3;
            }
            for (int v : cycle) {
                dfs2(v);
            }
            for (int v : comp) {
                deg[v] = 0;
            }

//            for (auto item : des) {
//                cerr << item << " ";
//            }
//            cerr << " des\n";

            for (int v : comp) {
                gg[v].resize(0);
                for (int u : g[v]) {
                    if (u < des[v]) {
//                        cerr << u << "<-" << v << "\n";
                        gg[v].push_back(u);
                        deg[u]++;
                    }
                }
            }
//            for (auto item : deg) {
//                cerr << item << " ";
//            }
//            cerr << " deg\n";
            int q = 0;
            for (int v : comp) {
                if (deg[v] == 0) {
                    int x = dfs3(v, -1);
                    q += x;
                    pp = mult(pp, dp[v]);
                    pp = mult(pp, calc_c(q, x));
//                    cerr << v << " " << x << "!\n";
                }
            }
//            for (auto item : dp) {
//                cerr << item << " ";
//            }
//            cerr << " dp\n";
            ss = (ss + pp) % MOD;
//            cerr << pp << " " << ss << "\n";
            std::reverse(cycle.begin(), cycle.end());
        }
        q += nn;
        res = mult(res, ss);
        res = mult(res, calc_c(q, nn));
    }
    cout << res << "\n";
    return 0;
}

Measured memory_bytes: 29432