//Create Time: 2018-05-07 21:51:12
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <sstream>
#include <fstream>
#include <functional>
#include <list>
#include <deque>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <cctype>
#include <ctime>
#include <utility>
#include <complex>
#include <cassert>
#include <climits>
#include <iterator>
#include <tuple>
using namespace std;
#define snuke(i,x) for (__typeof((x).begin()) i = (x).begin(); i != (x).end(); ++i)
#define rep(i,a,n) for (int i=(a);i<(n);i++)
#define per(i,n,a) for (int i=(n)-1;i>=(a);i--)
// #define OUT(x) std::cout<<(#x)<<":"<<(x)<<std::endl
// #define TST(x) std::cout<<(#x)<<":"<<(x)<<" "
#define OUT(args...) {string _s=#args;replace(_s.begin(),_s.end(),',',' ');stringstream _ss(_s);istream_iterator<string> _it(_ss);OUT_TST("\n",_it,args);}
#define TST(args...) {string _s=#args;replace(_s.begin(),_s.end(),',',' ');stringstream _ss(_s);istream_iterator<string> _it(_ss);OUT_TST("",_it,args);}
#define SZ(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
#define unq(x) sort(all(x)),(x).erase(unique(all(x)),(x).end())
#define mem(f,x) memset((f), (x), sizeof(f))
#define clr(f) mem(f,0)
#define x first
#define y second
#define mp make_pair
#define pb push_back
#define mt make_tuple
#define eb emplace_back
#define DEBUG
void OUT_TST(string s,istream_iterator<string> it) {cerr<<s;}
template<typename T, typename... Args> void OUT_TST(string s,istream_iterator<string> it,T a,Args... args){cerr<<*it<<"="<<a<<" ";OUT_TST(s,++it,args...);}
template<class T> void rd(T&ret){ret=0;bool ok=0,u=0;for(;;){int c=getchar();if(c>='0'&&c<='9')ret=(ret<<3)+(ret<<1)+c-'0',ok=1;else if(c=='-')u=1;else if(ok){if(u)ret*=-1;return;}}}
long long powmod(long long p,long long n,long long mod){long long ret=1;for(;n;n>>=1){if(n&1)ret=ret*p%mod;p=p*p%mod;}return ret;}
template <class T> bool chmin(T& a, const T &b) {return b < a? a = b, 1: 0;}
template <class T> bool chmax(T& a, const T &b) {return b > a? a = b, 1: 0;}
int gcd(int a,int b){return b?gcd(b,a%b):a;}
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii > vii;
typedef tuple<int,int,int> tp;
/****head****/
const ll mod=(ll)1e9+7;
const int N=(int)2e5+9;
int stk[N],top,loop[N],loop_top,dep[N],fa[N],cnt_e,clk,f1,lca,deg[N];
int isused[N],use[N];
bool vis[N],dp_vis[N];
vi eg[N],e[N];

template<int sz> struct Comb{
    const long long mod = ::mod;
    long long *fac, *ifac, *inv;
    long long mem_fac[sz+5],mem_ifac[sz+5],mem_inv[sz+5];

    // long long powmod(long long p,long long n,long long mod){long long ret=1;for(;n;n>>=1){if(n&1)ret=ret*p%mod;p=p*p%mod;}return ret;}

    Comb(long long *fa=0,long long *ifa=0,long long *iv=0){
        fac=fa?fa:mem_fac;
        ifac=ifa?ifa:mem_ifac;
        inv=iv?iv:mem_inv;
        fac[0]=fac[1]=1;
        ifac[0]=ifac[1]=1;
        inv[0]=inv[1]=1;  // set inv[0]=?
        for(int i=2;i<sz;++i){
            fac[i]=i*fac[i-1]%mod;
            inv[i]=(mod-mod/i)*inv[mod%i]%mod;
            ifac[i]=ifac[i-1]*inv[i]%mod;
        }
    }
    long long C(long long n,long long m){  // n >= m
        if(n<m)return 0;
        return (fac[n]*ifac[m]%mod)*ifac[n-m]%mod;
    }
    long long A(long long n,long long m){
        if(n<m)return 0;
        return fac[n]*ifac[n-m]%mod;
    }
};

long long fac[N],ifac[N],inv[N];
Comb<N> comb(fac,ifac,inv);

void dfs(int u,int f){
    stk[top++]=u;
    vis[u]=1;
    dep[u]=f==-1?0:dep[f]+1;
    fa[u]=f;
    for(int v:eg[u])if(v!=f){
        ++cnt_e;
        if(vis[v]){
            if(dep[v]<dep[u]){
                f1=u;lca=v;
            }
            continue;
        }
        dfs(v,u);
    }
}
pii dp(int u){
    dp_vis[u]=1;
    ll ans=1;
    int cnt=0,tcnt,tmp;
    for(int v:e[u]){
        tie(tmp,tcnt)=dp(v);
        ans*=comb.C(cnt+tcnt,cnt)*tmp%mod;
        ans%=mod;
        cnt+=tcnt;
    }
    return pii{ans,cnt+1};
}
pii calc(int u){
    top=0;
    cnt_e=0;
    f1=-1;

    dfs(u,-1);
    OUT(cnt_e);OUT(top);
    if(cnt_e-1!=top)return pii{0,top};
    assert(f1>=0);
    /*
    int la=f1,lb=f2;
    while(la!=lb){
        if(dep[la]>dep[lb])swap(la,lb);
        lb=fa[lb];
    }
    lca=la;
    */

    int t;

    loop_top=0;
    t=f1;
    while(t!=lca){
        loop[loop_top++]=t;
        t=fa[t];
    }
    loop[loop_top++]=lca;
    OUT(loop_top);
    rep(i,0,loop_top)TST(loop[i]);puts("");
    /*
    int idx=loop_top;
    t=f2;
    while(t!=lca){
        loop[loop_top++]=t;
        t=fa[t];
    }
    reverse(loop+idx,loop+loop_top);
    */

    auto getLR=[&](int x){
        int l=x-1,r=x+1;
        if(l<0)l+=loop_top;
        if(r==loop_top)r=0;
        return pii{l,r};
    };

    auto solve=[&](char dir){
        ++clk;
        rep(i,0,top)e[stk[i]].clear();
        clr(deg);

        int t;
        t=lca;
        while(fa[t]!=-1){
            isused[fa[t]]=clk;
            use[fa[t]]=t;
            t=fa[t];
        }

        int l,r;
        rep(i,0,loop_top){
            isused[loop[i]]=clk;
            tie(l,r)=getLR(i);
            if(dir=='l'){
                use[loop[i]]=loop[l];
            } else {
                use[loop[i]]=loop[r];
            }
        }

        rep(i,0,top){
            int x=stk[i];
            if(isused[x]==clk)continue;
            use[x]=fa[x];
        }
        rep(i,0,top){
            int x=stk[i];
            for(int v:eg[x])if(v<use[x]){
                e[x].pb(v);
                ++deg[v];
            }
        }

        /*
        OUT(dir);
        rep(i,0,top){
            int x=stk[i];
            TST(x);
            for(int v:e[x])TST(v);puts("");
        }
        */

        // clr(dp_vis);
        ll ans=1;
        int cnt=0,tmp,tcnt;
        rep(i,0,top)if(deg[stk[i]]==0){
            tie(tmp,tcnt)=dp(stk[i]);
            // OUT(stk[i],tmp,tcnt);
            ans*=comb.C(tcnt+cnt,cnt)*tmp%mod;
            ans%=mod;
            cnt+=tcnt;
        }
        assert(cnt==top);
        return ans;
    };

    int ans=solve('l') + solve('r');
    if(ans>=mod)ans-=mod;
    return pii{ans,top};
}
int main(){
    int n;cin>>n;
    rep(i,0,2*n){
        int a,b;scanf("%d%d",&a,&b);--a;--b;
        b+=n;
        // OUT(a,b);
        eg[a].pb(b);eg[b].pb(a);
    }
    ll ans=1;
    int cnt=0,tmp,tcnt;
    rep(i,0,n)if(!vis[i]){
        tie(tmp,tcnt)=calc(i);
        ans*=comb.C(cnt+tcnt,cnt)*tmp%mod;
        ans%=mod;
        cnt+=tcnt;
    }
    cout<<ans<<endl;
    return 0;
}


Measured memory_bytes: 32512