Language: C++

#include<bits/stdc++.h>
using namespace std;
#define LL long long
a#include<bits/stdc++.h>
using namespace std;
#define LL long long

const int maxn=2e5+10;
const int mo=1e9+7;

int n,root,Ans=1,tote=1;
int fa[maxn],fw[maxn],tmp[maxn],fac[maxn],ifac[maxn];
int FIR[maxn],TO[maxn<<1],NEXT[maxn<<1],W[maxn<<1];
int vis[maxn];
int q[maxn];

int power(int x,int k)
{
	int res=1;
	while (k)
	{
		if (k&1) res=(LL)res*x%mo;
		x=(LL)x*x%mo;
		k>>=1;
	}
	return res;
}

void addedge(int u,int v,int w)
{
	TO[++tote]=v;
	NEXT[tote]=FIR[u];
	W[tote]=w;
	FIR[u]=tote;
}

void dfs1(int u,int F)
{
	if (vis[u]==1) {root=u; return;}
	vis[u]=1;
	q[++q[0]]=u;
	for (int p=FIR[u];p;p=NEXT[p])
	{
		int v=TO[p];
		if (v==F) continue;
		dfs1(v,u);
	}
}

void dfs2(int u)
{
	if (vis[u]==2) return;
	vis[u]=2;
	for (int p=FIR[u];p;p=NEXT[p])
	{
		int v=TO[p],w=W[p];
		if (v==fa[u]) continue;
		fa[v]=u;
		fw[v]=w;
		dfs2(v);
	}
}

namespace Tree{
	int n,fa[maxn],dp[maxn],sz[maxn];
	int tote,FIR[maxn],TO[maxn<<1],NEXT[maxn<<1];

	void addedge(int u,int v)
	{
//		printf("%d----->%d\n",u,v);
		TO[++tote]=v;
		NEXT[tote]=FIR[u];
		FIR[u]=tote;
		fa[v]=u;
	}

	bool dfs(int u)
	{
//		printf("u=%d\n",u);
		if (dp[u]) return 0;
		sz[u]=0;
		dp[u]=1;
		for (int p=FIR[u];p;p=NEXT[p])
		{
			int v=TO[p];
			if (!dfs(v)) return 0;
			sz[u]+=sz[v];
			dp[u]=(LL)dp[u]*dp[v]%mo;
			dp[u]=(LL)dp[u]*ifac[sz[v]]%mo;
		}
		dp[u]=(LL)dp[u]*fac[sz[u]]%mo;
		sz[u]++;
		return 1;
	}

	void solve(int &Ans)
	{
		int i,u;

		for (i=1;i<=q[0];i++)
		{
			u=q[i];
			if (!fa[u]) addedge(0,u);
		}

		for (i=1;i<=q[0];i++)
		{
			u=q[i];
//			printf("%d : %d\n",u,fw[u]);
		}

		if (!dfs(0)) {Ans=0; return;}
		(Ans+=dp[0])%=mo;

//		printf("Ans=%d\nclear\n\n\n",Ans);

		for (i=1;i<=q[0];i++)
			u=q[i],FIR[u]=fa[u]=dp[u]=sz[u]=0;
		FIR[0]=fa[0]=dp[0]=sz[0]=0;
		tote=0;
	}
};

void dfs3(int u)
{
	if (vis[u]==3) return;
	vis[u]=3;
	for (int p=FIR[u];p;p=NEXT[p])
	{
		int v=TO[p],w=W[p];
		if (w<fw[u]) Tree::addedge(u,v);
		dfs3(v);
	}
}

void dfs4(int u)
{
	if (vis[u]==4) return;
	vis[u]=4;
	for (int p=FIR[u];p;p=NEXT[p])
	{
		int v=TO[p],w=W[p];
		if (w<fw[u]) Tree::addedge(u,v);
		dfs4(v);
	}
}

int main()
{
	int i,u,v;
	scanf("%d",&n);
	fac[0]=1;
	for (i=1;i<=n+n;i++)
		fac[i]=(LL)fac[i-1]*i%mo;
	ifac[n+n]=power(fac[n+n],mo-2);
	for (i=n+n;i>=1;i--)
		ifac[i-1]=(LL)ifac[i]*i%mo;
	for (i=1;i<=n+n;i++)
	{
		scanf("%d%d",&u,&v);
		addedge(u,n+v,u+v);
		addedge(n+v,u,u+v);
	}

	for (i=1;i<=n+n;i++)
		if (!vis[i])
		{
			q[0]=0;
			root=0;
			dfs1(i,0);
			if (!root) {puts("0"); return 0;}
			dfs2(root);

			int Sum=0;

			dfs3(root);
			Tree::solve(Sum);

			for (u=root;;u=fa[u])
			{
				tmp[fa[u]]=fw[u];
				if (fa[u]==root) break;
			}
			for (u=root;;u=fa[u])
			{
				fw[u]=tmp[u];
				if (fa[u]==root) break;
			}

			dfs4(root);
			Tree::solve(Sum);

			Ans=(LL)Ans*Sum%mo;
//			printf("q0=%d\n",q[0]);
			Ans=(LL)Ans*ifac[q[0]]%mo;
		}
	Ans=(LL)Ans*fac[n+n]%mo;
	printf("%d\n",Ans);
}

const int maxn=2e5+10;
const int mo=1e9+7;

int n,root,Ans=1,tote=1;
int fa[maxn],fw[maxn],tmp[maxn],fac[maxn],ifac[maxn];
int FIR[maxn],TO[maxn<<1],NEXT[maxn<<1],W[maxn<<1];
int vis[maxn];
int q[maxn];

int power(int x,int k)
{
	int res=1;
	while (k)
	{
		if (k&1) res=(LL)res*x%mo;
		x=(LL)x*x%mo;
		k>>=1;
	}
	return res;
}

void addedge(int u,int v,int w)
{
	TO[++tote]=v;
	NEXT[tote]=FIR[u];
	W[tote]=w;
	FIR[u]=tote;
}

void dfs1(int u,int F)
{
	if (vis[u]) {root=u; return;}
	q[++q[0]]=u;
	vis[u]=1;
	for (int p=FIR[u];p;p=NEXT[p])
	{
		int v=TO[p];
		if (v==F) continue;
		dfs1(v,u);
	}
}

void dfs2(int u)
{
	if (vis[u]==2) return;
	vis[u]=2;
	for (int p=FIR[u];p;p=NEXT[p])
	{
		int v=TO[p],w=W[p];
		if (v==fa[u]) continue;
		fa[v]=u;
		fw[v]=w;
		dfs2(v);
	}
}

namespace Tree{
	int n,fa[maxn],dp[maxn],sz[maxn];
	int tote,FIR[maxn],TO[maxn<<1],NEXT[maxn<<1];

	void addedge(int u,int v)
	{
//		printf("%d----->%d\n",u,v);
		TO[++tote]=v;
		NEXT[tote]=FIR[u];
		FIR[u]=tote;
		fa[v]=u;
	}

	void dfs(int u)
	{
//		printf("u=%d\n",u);
		sz[u]=0;
		dp[u]=1;
		for (int p=FIR[u];p;p=NEXT[p])
		{
			int v=TO[p];
			dfs(v);
			sz[u]+=sz[v];
			dp[u]=(LL)dp[u]*dp[v]%mo;
			dp[u]=(LL)dp[u]*ifac[sz[v]]%mo;
		}
		dp[u]=(LL)dp[u]*fac[sz[u]]%mo;
		sz[u]++;
	}

	void solve(int &Ans)
	{
		int i,u;

		for (i=1;i<=q[0];i++)
		{
			u=q[i];
			if (!fa[u]) addedge(0,u);
		}

		for (i=1;i<=q[0];i++)
		{
			u=q[i];
//			printf("%d : %d\n",u,fw[u]);
		}

		dfs(0);
		(Ans+=dp[0])%=mo;

//		printf("Ans=%d\nclear\n\n\n",Ans);

		for (i=1;i<=q[0];i++)
			u=q[i],FIR[u]=fa[u]=dp[u]=sz[u]=0;
		FIR[0]=fa[0]=dp[0]=sz[0]=0;
		tote=0;
	}
};

void dfs3(int u)
{
	if (vis[u]==3) return;
	vis[u]=3;
	for (int p=FIR[u];p;p=NEXT[p])
	{
		int v=TO[p],w=W[p];
		if (w<fw[u]) Tree::addedge(u,v);
		dfs3(v);
	}
}

void dfs4(int u)
{
	if (vis[u]==4) return;
	vis[u]=4;
	for (int p=FIR[u];p;p=NEXT[p])
	{
		int v=TO[p],w=W[p];
		if (w<fw[u]) Tree::addedge(u,v);
		dfs4(v);
	}
}

int main()
{
	int i,u,v;
	scanf("%d",&n);
	fac[0]=1;
	for (i=1;i<=n+n;i++)
		fac[i]=(LL)fac[i-1]*i%mo;
	ifac[n+n]=power(fac[n+n],mo-2);
	for (i=n+n;i>=1;i--)
		ifac[i-1]=(LL)ifac[i]*i%mo;
	for (i=1;i<=n+n;i++)
	{
		scanf("%d%d",&u,&v);
		addedge(u,n+v,u+v);
		addedge(n+v,u,u+v);
	}

	for (i=1;i<=n+n;i++)
		if (!vis[i])
		{
			q[0]=0;
			root=0;
			dfs1(i,0);
			if (!root) {puts("0"); return 0;}
			dfs2(root);

			int Sum=0;

			dfs3(root);
			Tree::solve(Sum);

			for (u=root;;u=fa[u])
			{
				tmp[fa[u]]=fw[u];
				if (fa[u]==root) break;
			}
			for (u=root;;u=fa[u])
			{
				fw[u]=tmp[u];
				if (fa[u]==root) break;
			}

			dfs4(root);
			Tree::solve(Sum);

			Ans=(LL)Ans*Sum%mo;
//			printf("q0=%d\n",q[0]);
			Ans=(LL)Ans*ifac[q[0]]%mo;
		}
	Ans=(LL)Ans*fac[n+n]%mo;
	printf("%d\n",Ans);
}

Compiler output:
a.cc:4:2: error: stray '#' in program
    4 | a#include<bits/stdc++.h>
      |  ^
a.cc:4:1: error: 'a' does not name a type
    4 | a#include<bits/stdc++.h>
      | ^
a.cc:201:11: error: redefinition of 'const int maxn'
  201 | const int maxn=2e5+10;
      |           ^~~~
a.cc:8:11: note: 'const int maxn' previously defined here
    8 | const int maxn=2e5+10;
      |           ^~~~
a.cc:202:11: error: redefinition of 'const int mo'
  202 | const int mo=1e9+7;
      |           ^~
a.cc:9:11: note: 'const int mo' previously defined here
    9 | const int mo=1e9+7;
      |           ^~
a.cc:204:5: error: redefinition of 'int n'
  204 | int n,root,Ans=1,tote=1;
      |     ^
a.cc:11:5: note: 'int n' previously declared here
   11 | int n,root,Ans=1,tote=1;
      |     ^
a.cc:204:7: error: redefinition of 'int root'
  204 | int n,root,Ans=1,tote=1;
      |       ^~~~
a.cc:11:7: note: 'int root' previously declared here
   11 | int n,root,Ans=1,tote=1;
      |       ^~~~
a.cc:204:12: error: redefinition of 'int Ans'
  204 | int n,root,Ans=1,tote=1;
      |            ^~~
a.cc:11:12: note: 'int Ans' previously defined here
   11 | int n,root,Ans=1,tote=1;
      |            ^~~
a.cc:204:18: error: redefinition of 'int tote'
  204 | int n,root,Ans=1,tote=1;
      |                  ^~~~
a.cc:11:18: note: 'int tote' previously defined here
   11 | int n,root,Ans=1,tote=1;
      |                  ^~~~
a.cc:205:5: error: redefinition of 'int fa [200010]'
  205 | int fa[maxn],fw[maxn],tmp[maxn],fac[maxn],ifac[maxn];
      |     ^~
a.cc:12:5: note: 'int fa [200010]' previously declared here
   12 | int fa[maxn],fw[maxn],tmp[maxn],fac[maxn],ifac[maxn];
      |     ^~
a.cc:205:14: error: redefinition of 'int fw [200010]'
  205 | int fa[maxn],fw[maxn],tmp[maxn],fac[maxn],ifac[maxn];
      |              ^~
a.cc:12:14: note: 'int fw [200010]' previously declared here
   12 | int fa[maxn],fw[maxn],tmp[maxn],fac[maxn],ifac[maxn];
      |              ^~
a.cc:205:23: error: redefinition of 'int tmp [200010]'
  205 | int fa[maxn],fw[maxn],tmp[maxn],fac[maxn],ifac[maxn];
      |                       ^~~
a.cc:12:23: note: 'int tmp [200010]' previously declared here
   12 | int fa[maxn],fw[maxn],tmp[maxn],fac[maxn],ifac[maxn];
      |                       ^~~
a.cc:205:33: error: redefinition of 'int fac [200010]'
  205 | int fa[maxn],fw[maxn],tmp[maxn],fac[maxn],ifac[maxn];
      |                                 ^~~
a.cc:12:33: note: 'int fac [200010]' previously declared here
   12 | int fa[maxn],fw[maxn],tmp[maxn],fac[maxn],ifac[maxn];
      |                                 ^~~
a.cc:205:43: error: redefinition of 'int ifac [200010]'
  205 | int fa[maxn],fw[maxn],tmp[maxn],fac[maxn],ifac[maxn];
      |                                           ^~~~
a.cc:12:43: note: 'int ifac [200010]' previously declared here
   12 | int fa[maxn],fw[maxn],tmp[maxn],fac[maxn],ifac[maxn];
      |                                           ^~~~
a.cc:206:5: error: redefinition of 'int FIR [200010]'
  206 | int FIR[maxn],TO[maxn<<1],NEXT[maxn<<1],W[maxn<<1];
      |     ^~~
a.cc:13:5: note: 'int FIR [200010]' previously declared here
   13 | int FIR[maxn],TO[maxn<<1],NEXT[maxn<<1],W[maxn<<1];
      |     ^~~
a.cc:206:15: error: redefinition of 'int TO [400020]'
  206 | int FIR[maxn],TO[maxn<<1],NEXT[maxn<<1],W[maxn<<1];
      |               ^~
a.cc:13:15: note: 'int TO [400020]' previously declared here
   13 | int FIR[maxn],TO[maxn<<1],NEXT[maxn<<1],W[maxn<<1];
      |               ^~
a.cc:206:27: error: redefinition of 'int NEXT [400020]'
  206 | int FIR[maxn],TO[maxn<<1],NEXT[maxn<<1],W[maxn<<1];
      |                           ^~~~
a.cc:13:27: note: 'int NEXT [400020]' previously declared here
   13 | int FIR[maxn],TO[maxn<<1],NEXT[maxn<<1],W[maxn<<1];
      |                           ^~~~
a.cc:206:41: error: redefinition of 'int W [400020]'
  206 | int FIR[maxn],TO[maxn<<1],NEXT[maxn<<1],W[maxn<<1];
      |                                         ^
a.cc:13:41: note: 'int W [400020]' previously declared here
   13 | int FIR[maxn],TO[maxn<<1],NEXT[maxn<<1],W[maxn<<1];
      |                                         ^
a.cc:207:5: error: redefinition of 'int vis [200010]'
  207 | int vis[maxn];
      |     ^~~
a.cc:14:5: note: 'int vis [200010]' previously declared here
   14 | int vis[maxn];
      |     ^~~
a.cc:208:5: error: redefinition of 'int q [200010]'
  208 | int q[maxn];
      |     ^
a.cc:15:5: note: 'int q [200010]' previously declared here
   15 | int q[maxn];
      |     ^
a.cc:210:5: error: redefinition of 'int power(int, int)'
  210 | int power(int x,int k)
      |     ^~~~~
a.cc:17:5: note: 'int power(int, int)' previously defined here
   17 | int power(int x,int k)
      |     ^~~~~
a.cc:222:6: error: redefinition of 'void addedge(int, int, int)'
  222 | void addedge(int u,int v,int w)
      |      ^~~~~~~
a.cc:29:6: note: 'void addedge(int, int, int)' previously defined here
   29 | void addedge(int u,int v,int w)
      |      ^~~~~~~
a.cc:230:6: error: redefinition of 'void dfs1(int, int)'
  230 | void dfs1(int u,int F)
      |      ^~~~
a.cc:37:6: note: 'void dfs1(int, int)' previously defined here
   37 | void dfs1(int u,int F)
      |      ^~~~
a.cc:243:6: error: redefinition of 'void dfs2(int)'
  243 | void dfs2(int u)
      |      ^~~~
a.cc:50:6: note: 'void dfs2(int)' previously defined here
   50 | void dfs2(int u)
      |      ^~~~
a.cc:258:13: error: redefinition of 'int Tree::n'
  258 |         int n,fa[maxn],dp[maxn],sz[maxn];
      |             ^
a.cc:65:13: note: 'int Tree::n' previously declared here
   65 |         int n,fa[maxn],dp[maxn],sz[maxn];
      |             ^
a.cc:258:15: error: redefinition of 'int Tree::fa [200010]'
  258 |         int n,fa[maxn],dp[maxn],sz[maxn];
      |               ^~
a.cc:65:15: note: 'int Tree::fa [200010]' previously declared here
   65 |         int n,fa[maxn],dp[maxn],sz[maxn];
      |               ^~
a.cc:258:24: error: redefinition of 'int Tree::dp [200010]'
  258 |         int n,fa[maxn],dp[maxn],sz[maxn];
      |                        ^~
a.cc:65:24: note: 'int Tree::dp [200010]' previously declared here
   65 |         int n,fa[maxn],dp[maxn],sz[maxn];
      |                        ^~
a.cc:258:33: error: redefinition of 'int Tree::sz [200010]'
  258 |         int n,fa[maxn],dp[maxn],sz[maxn];
      |                                 ^~
a.cc:65:33: note: 'int Tree::sz [200010]' previously declared here
   65 |         int n,fa[maxn],dp[maxn],sz[maxn];
      |                                 ^~
a.cc:259:13: error: redefinition of 'int Tree::tote'
  259 |         int tote,FIR[maxn],TO[maxn<<1],NEXT[maxn<<1];
      |             ^~~~
a.cc:66:13: note: 'int Tree::tote' previously declared here
   66 |         int tote,FIR[maxn],TO[maxn<<1],NEXT[maxn<<1];
      |             ^~~~
a.cc:259:18: error: redefinition of 'int Tree::FIR [200010]'
  259 |         int tote,FIR[maxn],TO[maxn<<1],NEXT[maxn<<1];
      |                  ^~~
a.cc:66:18: note: 'int Tree::FIR [200010]' previously declared here
   66 |         int tote,FIR[maxn],TO[maxn<<1],NEXT[maxn<<1];
      |                  ^~~
a.cc:259:28: error: redefinition of 'int Tree::TO [400020]'
  259 |         int tote,FIR[maxn],TO[maxn<<1],NEXT[maxn<<1];
      |                            ^~
a.cc:66:28: note: 'int Tree::TO [400020]' previously declared here
   66 |         int tote,FIR[maxn],TO[maxn<<1],NEXT[maxn<<1];
      |                            ^~
a.cc:259:40: error: redefinition of 'int Tree::NEXT [400020]'
  259 |         int tote,FIR[maxn],TO[maxn<<1],NEXT[maxn<<1];
      |                                        ^~~~
a.cc:66:40: note: 'int Tree::NEXT [400020]' previously declared here
   66 |         int tote,FIR[maxn],TO[maxn<<1],NEXT[maxn<<1];
      |                                        ^~~~
a.cc:261:14: error: redefinition of 'void Tree::addedge(int, int)'
  261 |         void addedge(int u,int v)
      |              ^~~~~~~
a.cc:68:14: note: 'void Tree::addedge(int, int)' previously defined here
   68 |         void addedge(int u,int v)
      |              ^~~~~~~
a.cc:270:14: error: ambiguating new declaration of 'void Tree::dfs(int)'
  270 |         void dfs(int u)
      |              ^~~
a.cc:77:14: note: old declaration 'bool Tree::dfs(int)'
   77 |         bool dfs(int u)
      |              ^~~
a.cc:287:14: error: redefinition of 'void Tree::solve(int&)'
  287 |         void solve(int &Ans)
      |              ^~~~~
a.cc:96:14: note: 'void Tree::solve(int&)' previously defined here
   96 |         void solve(int &Ans)
      |              ^~~~~
a.cc:315:6: error: redefinition of 'void dfs3(int)'
  315 | void dfs3(int u)
      |      ^~~~
a.cc:124:6: note: 'void dfs3(int)' previously defined here
  124 | void dfs3(int u)
      |      ^~~~
a.cc:327:6: error: redefinition of 'void dfs4(int)'
  327 | void dfs4(int u)
      |      ^~~~
a.cc:136:6: note: 'void dfs4(int)' previously defined here
  136 | void dfs4(int u)
      |      ^~~~
a.cc:339:5: error: redefinition of 'int main()'
  339 | int main()
      |     ^~~~
a.cc:148:5: note: 'int main()' previously defined here
  148 | int main()
      |     ^~~~