#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcountll

#define INF 1e16
#define mod 1000000007

ll fac[200010],finv[200010];

ll mod_pow(ll a,ll n){
  ll res=1;
  while(n>0){
    if(n&1)res=res*a%mod;
    a=a*a%mod;
    n>>=1;
  }
  return res;
}

ll merge(ll a,ll b,ll asz,ll bsz){
  ll f=((finv[asz]*finv[bsz])%mod)*fac[asz+bsz]%mod;
  return ((f*a)%mod)*b%mod;
}

int N;
vector<int> g[200001],t[200010];
bool vis1[200010],vis2[200010];
ll ecnt=0;
vector<int> vs,loop;
int nxt[200001],indeg[200010];
ll tsz[200010];

ll predfs(int v){
  vs.push_back(v);
  vis1[v]=true;
  ll sz=1;
  for(int nv : g[v]){
    ecnt++;
    if(vis1[nv])continue;
    sz+=predfs(nv);
  }
  return sz;
}

int dfs(int v,int pre){
  vis2[v]=true;
  for(int nv : g[v]){
    if(nv==pre)continue;
    if(vis2[nv]){
      loop.push_back(nv); return nv;
    }
    int tmp=dfs(nv,v);
    if(tmp!=-1){
      if(nv==tmp) return -1;
      else{
        loop.push_back(nv); return tmp;
      }
    }
  }
  return -1;
}

ll dfs2(int v){
  vis2[v]=true;
  ll res=1;
  tsz[v]=0;
  for(int nv : t[v]){
    ll tmp=dfs2(nv);
    res=merge(res,tmp,tsz[v],tsz[nv]);
    tsz[v]+=tsz[nv];
  }
  tsz[v]++;
  return res;
}

void dfs3(int v,int pre){
  nxt[v]=pre;
  for(int nv : g[v]){
    if(nv==pre)continue;
    dfs3(nv,v);
  }
}

ll calc(int v){
  int V=vs.size();
  rep(i,V)vis2[vs[i]]=false;
  loop.clear();
  dfs(v,-1);
  int L=loop.size();

  ll sum=0;
  ll res1=1,res2=1;
  { // +
    rep(i,V)nxt[vs[i]]=-1;
    rep(i,L){
      nxt[loop[i]]=loop[(i+1)%L];
      int crt=loop[i];
      for(int nv : g[crt]){
        if(nv==loop[(i+1)%L]||nv==loop[(i-1+L)%L])continue;
        dfs3(nv,crt);
      }
    }
    rep(i,V){
      int crt=vs[i];
      for(int nv : g[crt]){
        if(nv<nxt[crt]){
          t[crt].push_back(nv); indeg[nv]++;
        }
      }
    }
    rep(i,V)vis2[vs[i]]=false;
    rep(i,V){
      if(vis2[vs[i]]||indeg[vs[i]]>0)continue;
      ll add=dfs2(vs[i]);
      res1=merge(res1,add,sum,tsz[vs[i]]);
      sum+=tsz[vs[i]];
    }
  }

  sum=0;
  { // +
    rep(i,V){
      nxt[vs[i]]=-1; indeg[vs[i]]=0;
      t[vs[i]].clear(); tsz[vs[i]]=0;
    }
    rep(i,L){
      nxt[loop[i]]=loop[(i-1+L)%L];
      int crt=loop[i];
      for(int nv : g[crt]){
        if(nv==loop[(i+1)%L]||nv==loop[(i-1+L)%L])continue;
        dfs3(nv,crt);
      }
    }
    rep(i,V){
      int crt=vs[i];
      for(int nv : g[crt]){
        if(nv<nxt[crt]){
          t[crt].push_back(nv); indeg[nv]++;
        }
      }
    }
    rep(i,V)vis2[vs[i]]=false;
    rep(i,V){
      if(vis2[vs[i]]||indeg[vs[i]]>0)continue;
      ll add=dfs2(vs[i]);
      res2=merge(res2,add,sum,tsz[vs[i]]);
      sum+=tsz[vs[i]];
    }
  }

  return (res1+res2)%mod;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  fac[0]=1;
  rep(i,200000)fac[i+1]=fac[i]*(i+1)%mod;
  rep(i,200001)finv[i]=mod_pow(fac[i],mod-2);

  cin>>N;
  rep(i,2*N){
    int x,y;
    cin>>x>>y;
    x--;y--;
    g[x].push_back(N+y);
    g[N+y].push_back(x);
  }

  ll res=1;
  ll sum=0;
  rep(i,2*N){
    if(vis1[i])continue;
    vs.clear(); ecnt=0;
    ll sz=predfs(i); ecnt/=2;
    if(sz!=ecnt) res=0;
    else res=merge(res,calc(i),sum,sz);
    sum+=sz;
  }
  cout<<res<<endl;

  return 0;
}


Measured memory_bytes: 30584