#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
vector<int>E[201000];
int n, ss, v[201000], Q[201000], head, tail, Deg[201000], Nxt[201000], Mod = 1000000007, C[201000];
int F[201000], InvF[201000];
long long D[201000];
vector<int>T, G[201000];
void DFS(int a){
    T.push_back(a);
    ss += E[a].size();
    v[a] = 1;
    for(int i=0;i<E[a].size();i++)if(!v[E[a][i]])DFS(E[a][i]);
}
void DP(int a){
    int i;
    int r = 1;
    C[a] = 0;
    for(i=0;i<G[a].size();i++){
        DP(G[a][i]);
        r = 1ll * r * D[G[a][i]] % Mod * InvF[C[G[a][i]]] % Mod;
        C[a] += C[G[a][i]];
    }
    D[a] = 1ll * r * F[C[a]]%Mod;
    C[a]++;
}
long long Get(int st, int a){
    Nxt[st] = a;
    int pp = st, i;
    while(a != st){
        for(i=0;i<E[a].size();i++){
            if(Deg[E[a][i]] > 1 && pp != E[a][i])break;
        }
        pp = a;
        Nxt[a] = E[a][i];
        a = E[a][i];
    }
    for(i=0;i<T.size();i++)G[T[i]].clear();
    G[0].clear();
    for(i=0;i<T.size();i++){
        if(T[i] < Nxt[Nxt[T[i]]]) G[Nxt[T[i]]].push_back(T[i]);
        else G[0].push_back(T[i]);
    }
    DP(0);
    return D[0];
}
int Res = 1;
void Calc(){
    int i;
    head = tail = 0;
    for(i=0;i<T.size();i++){
        if(Deg[T[i]] == 1)Q[++tail] = T[i];
    }
    int x;
    while(head < tail){
        x = Q[++head];
        for(i=0;i<E[x].size();i++){
            if(Deg[E[x][i]] > 1){
                Nxt[x] = E[x][i];
                Deg[E[x][i]]--;
                if(Deg[E[x][i]] == 1)Q[++tail] = E[x][i];
            }
        }
    }
    for(i=0;i<T.size();i++){
        if(Deg[T[i]] > 1)break;
    }
    x = T[i];
    int t1 = -1, t2 = -1;
    for(i=0;i<E[x].size();i++){
        if(Deg[E[x][i]] > 1){
            if(t1 == -1)t1 = E[x][i];
            else t2 = E[x][i];
        }
    }
    int s = 0;
    s += Get(x, t1);
    s += Get(x, t2);
    Res = 1ll * Res * s % Mod * InvF[T.size()] % Mod;
}
int Pow(int a, int b){
    int r = 1;
    while(b){
        if(b&1)r=1ll*r*a%Mod;
        a=1ll*a*a%Mod;b>>=1;
    }
    return r;
}
int main(){
    int i, x, y;
    scanf("%d",&n);
    F[0] = 1;
    for(i=1;i<=n+n;i++)F[i] = 1ll*F[i-1]*i%Mod;
    InvF[n+n] = Pow(F[n+n], Mod-2);
    for(i=n+n-1;i>=0;i--)InvF[i] = 1ll*InvF[i+1]*(i+1)%Mod;
    for(i=1;i<=n+n;i++){
        scanf("%d%d",&x,&y);
        E[x].push_back(n+y);
        E[n+y].push_back(x);
        Deg[x]++,Deg[n+y]++;
    }
    for(i=1;i<=n+n;i++){
        if(!v[i]){
            ss = 0;
            T.clear();
            DFS(i);
            if(ss != T.size()*2){
                printf("0\n");
                return 0;
            }
            Calc();
        }
    }
    Res = 1ll * Res * F[n+n]%Mod;
    printf("%d\n",Res);
}