Language: C++

#include <cstdio>
#include <vector>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define pb push_back
#define ref(i,x,y)for(int i=x;i<=y;++i)
#define def(i,x,y)for(int i=x;i>=y;--i)
typedef long long LL;
int read(){
	char c=getchar();int d=0,f=1;
	for(;c<'0'||c>'9';c=getchar())if(c=='-')f=-1;
	for(;c>='0'&&c<='9';d=d*10+c-48,c=getchar());
	return d*f;
}
const int N=200100,mod=1e9+7;
bool instk[N];
int n,o,top,tmpr,ans,w[N],res,f[N],mkx[N],mky[N],F[N],vis[N],stk[N],h[N],t[N];
struct xint{int x,y;}p[N];
vector<int> vx[N],vy[N],V[N];
bool cmpx(int a,int b){return p[a].x<p[b].x;}
bool cmpy(int a,int b){return p[a].y<p[b].y;}
void nosolution(){
	printf("0\n");exit(0);
}

int dep;

void dfs(int f,int x){


	if(instk[x]){
		for(int i=top;;i--){
			h[++h[0]]=stk[i];
			if(stk[i]==x)break;
		}return;
	}
	if(vis[x])return;
	
	
	
dep++;
if(dep>200000)return 0;
	
	if(!mkx[p[x].x])tmpr++;mkx[p[x].x]=1;
	if(!mky[p[x].y])tmpr++;mky[p[x].y]=1;
	tmpr--;
	vis[x]=1;stk[++top]=x;instk[x]=1;
	ref(i,0,V[x].size()-1)if(V[x][i]!=f)
		dfs(x,V[x][i]);
	stk[top--]=0;instk[x]=0;
	
dep--;	
	
}
int dfs(int x){


dep++;
if(dep>200000)return;

	t[x]=0;
	int X=p[x].x,Y=p[x].y,sum=1;
	if(w[x]==1){
		ref(i,0,vx[X].size()-1)if(vx[X][i]!=x&&vx[X][i]!=o){
			int y=vx[X][i];w[y]=2;
			int s=dfs(y);
			if(p[y].y<Y){
				t[x]+=t[y];
				sum=(LL)sum*F[t[y]]%mod*s%mod;
			}else res=(LL)res*F[t[y]]%mod*s%mod;
		}
	}
	if(w[x]==2){
		ref(i,0,vy[Y].size()-1)if(vy[Y][i]!=x&&vy[Y][i]!=o){
			int y=vy[Y][i];w[y]=1;
			int s=dfs(y);
			if(p[y].x<X){
				t[x]+=t[y];
				sum=(LL)sum*F[t[y]]%mod*s%mod;
			}else res=(LL)res*F[t[y]]%mod*s%mod;
		}
	}
	sum=(LL)sum*f[t[x]]%mod;
	t[x]++;
	
dep--;

	return sum;
}
void work(int x){
	tmpr=0;h[0]=0;dfs(0,x);
	if(tmpr!=0)nosolution();
	o=0;int xx,yy,s=0,Res=0;res=1;
	ref(i,1,h[0])if(!o||xx<p[h[i]].x||xx==p[h[i]].x&&yy<p[h[i]].y)
		o=h[i],xx=p[h[i]].x,yy=p[h[i]].y;
	w[o]=1;s=dfs(o);Res=(Res+(LL)res*F[t[o]]%mod*s%mod)%mod;res=1;
	w[o]=2;s=dfs(o);Res=(Res+(LL)res*F[t[o]]%mod*s%mod)%mod;
	ans=(LL)ans*Res%mod;
}
int mi(int a,int b){
	int s=1;
	for(;b;b>>=1,a=(LL)a*a%mod)
		if(b&1)s=(LL)s*a%mod;
	return s;
}
void init(){
	f[0]=F[0]=1;
	ref(i,1,n)f[i]=(LL)f[i-1]*i%mod;
	F[n]=mi(f[n],mod-2);
	def(i,n-1,1)F[i]=(LL)F[i+1]*(i+1)%mod;
}
int main(){
	n=read();n=n*2;
	ref(i,1,n)p[i].x=read(),p[i].y=read();
	ref(i,1,n)vx[p[i].x].pb(i),vy[p[i].y].pb(i);
	ref(i,1,n/2)if(!vx[i].empty()){
		sort(vx[i].begin(),vx[i].end(),cmpy);
		int s=vx[i].size();
		ref(j,0,s-2)V[vx[i][j]].pb(vx[i][j+1]);
		ref(j,0,s-2)V[vx[i][j+1]].pb(vx[i][j]);
	}
	ref(i,1,n/2)if(!vy[i].empty()){
		sort(vy[i].begin(),vy[i].end(),cmpx);
		int s=vy[i].size();
		ref(j,0,s-2)V[vy[i][j]].pb(vy[i][j+1]);
		ref(j,0,s-2)V[vy[i][j+1]].pb(vy[i][j]);
	}
	init();ans=f[n];
	ref(i,1,n)if(!vis[i])work(i);
	printf("%d\n",ans);
}

Compiler output:
a.cc: In function 'void dfs(int, int)':
a.cc:44:22: error: return-statement with a value, in function returning 'void' [-fpermissive]
   44 | if(dep>200000)return 0;
      |                      ^
a.cc: In function 'int dfs(int)':
a.cc:61:15: error: return-statement with no value, in function returning 'int' [-fpermissive]
   61 | if(dep>200000)return;
      |               ^~~~~~