// Copyright (C) 2017 __debug.

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; version 3

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; If not, see <http://www.gnu.org/licenses/>.


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>

#define x first
#define y second
#define MP std::make_pair
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()
#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#ifdef __linux__
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif

using std::pair;
using std::vector;
using std::string;

typedef long long LL;
typedef pair<int, int> Pii;

const int oo = 0x3f3f3f3f;

template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, true : false; }
template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, true : false; }
string procStatus()
{
    std::ifstream t("/proc/self/status");
    return string(std::istreambuf_iterator<char>(t), std::istreambuf_iterator<char>());
}
template<typename T> T read(T &x)
{
    int f = 1;
    char ch = getchar();
    for (; !isdigit(ch); ch = getchar())
        f = (ch == '-' ? -1 : 1);
    for (x = 0; isdigit(ch); ch = getchar())
        x = 10 * x + ch - '0';
    return x *= f;
}
template<typename T> void write(T x)
{
    if (x == 0) {
        putchar('0');
        return;
    }
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    static char s[20];
    int top = 0;
    for (; x; x /= 10)
        s[++top] = x % 10 + '0';
    while (top)
        putchar(s[top--]);
}
// EOT

const int MAXN = 2e5 + 5;
const int MOD = 1e9 + 7;

int N;
Pii A[MAXN];

void input()
{
    read(N);
    N *= 2;
    for (int i = 1; i <= N; ++i) {
        read(A[i].x); read(A[i].y);
    }
}

std::map<Pii, int> id;
vector<int> G[MAXN];
vector<int> cyc;
vector<Pii> edges;
int cycflag;
int dep[MAXN];
int st[MAXN];
int top;

LL fpm(LL base, LL exp)
{
    LL ret = 1;
    for (; exp; exp >>= 1) {
        if (exp & 1)
            (ret *= base) %= MOD;
        (base *= base) %= MOD;
    }
    return ret;
}

void dfs(int u, int f)
{
    dep[u] = dep[f] + 1;
    for (int i = 0; i < SZ(G[u]); ++i) {
        int v = G[u][i];
        if (v == f)
            continue;
        if (!dep[v]) {
            st[++top] = SZ(edges);
            edges.push_back(MP(v, u));
            dfs(v, u);
            --top;
        } else if (dep[v] < dep[u]) {
            if (++cycflag == 1) {
                int t = top;
                do {
                    cyc.push_back(st[t]);
                } while (edges[st[t--]].y != v);
                cyc.push_back(SZ(edges));
                edges.push_back(MP(v, u));
            } else {
                puts("0");
                exit(0);
            }
        }
    }
}

namespace MickeyMouseClubhouse
{

vector<int> T[MAXN];
bool vis[MAXN], rt[MAXN];
int size[MAXN];

LL dfs(int u)
{
    LL ret = 1;
    size[u] = 1;
    vis[u] = true;
    for (int i = 0; i < SZ(T[u]); ++i) {
        int v = T[u][i];
        if (vis[v] || !((ret *= dfs(v)) %= MOD))
            return 0;
        size[u] += size[v];
    }
    return ret * fpm(size[u], MOD - 2) % MOD;
}

LL count()
{
    std::set<int> V;

    for (int i = 0; i < SZ(edges); ++i) {
        int u = edges[i].x, v = edges[i].y;
        int t = std::lower_bound(ALL(G[u]), v) - G[u].begin();
        int y = id[MP(u, v)];
        if (V.insert(y).y) {
            T[y].clear();
            vis[y] = false;
            rt[y] = true;
        }
        while (t--) {
            int x = id[MP(u, G[u][t])];
            if (V.insert(x).y) {
                T[x].clear();
                vis[x] = false;
            }
            T[y].push_back(x);
            rt[x] = false;
        }
    }

    LL ret = 1;
    for (auto x : V) {
        if (rt[x])
            (ret *= dfs(x)) %= MOD;
    }

    return ret;
}

}

void solve()
{
    std::sort(A + 1, A + N + 1);
    for (int i = 1; i <= N; ++i) {
        G[A[i].x].push_back(A[i].y + N / 2);
        G[A[i].y + N / 2].push_back(A[i].x);
        // printf("%d %d %d\n", A[i].x, A[i].y + N / 2, i);
        id[MP(A[i].x, A[i].y + N / 2)] = i;
        id[MP(A[i].y + N / 2, A[i].x)] = i;
    }

    LL ans = 1;
    for (int i = 1; i <= N; ++i) {
        if (dep[i])
            continue;
        cycflag = 0; edges.clear(); cyc.clear();
        dfs(i, 0);
        if (cycflag != 1) {
            puts("0");
            return;
        }
        int rt = edges[cyc[0]].x;
        for (auto e : edges) {
            dep[e.x] = dep[e.y] = 0;
        }
        cycflag = 0; edges.clear(); cyc.clear();
        dfs(rt, 0);

        LL cur = 0;
        (cur += MickeyMouseClubhouse::count()) %= MOD;
        for (int j = 0; j < SZ(cyc); ++j) {
            std::swap(edges[cyc[j]].x, edges[cyc[j]].y);
        }
        (cur += MickeyMouseClubhouse::count()) %= MOD;

        (ans *= cur) %= MOD;
    }

    for (int i = 1; i <= N; ++i) {
        (ans *= i) %= MOD;
    }

    printf("%lld\n", ans);
}

int main()
{
#ifdef __DEBUG
    freopen("F.in", "r", stdin);
    freopen("F.out", "w", stdout);
#endif

    input();
    solve();
}

// 优化原则：雕琢前先得有原型，跑之前先学会走。
//     -- UNIX哲学基础第十五条，《UNIX编程艺术》by ESR


Measured memory_bytes: 65904