Language: C++

#include<bits/stdc++.h>
#define mo 1000000007
using namespace std;
int fac[100010],nifac[100010],e[100010];
int first[100010],next[100010],des[100010],w[100010];
int size[100010],ans,f[100010],vn,en,ed;
bool vis[100010];
int tt,n;
int C(int n,int m){return (long long)fac[n]*nifac[m]%mo*nifac[n-m]%mo;}
int getmi(int a,int x)
{
	int ans=1;
	while (x)
	{
		if (x&1) ans=(long long)ans*a%mo;
		a=(long long)a*a%mo;
		x>>=1;
	}
	return ans;
}
void dfs(int s,int pre)
{
	vis[s]=true;vn++;
	for (int k=first[s];k;k=next[k])
	{
		en++;
		if (!vis[des[k]]) {e[des[k]]=k^1;dfs(des[k],s);}
		else if (des[k]!=pre) ed=k;
	}
}
void dp(int s,int e)
{
	f[s]=1;size[s]=0;
	for (int k=first[s];k;k=next[k]) if (k/2!=ed/2&&(k^1^e))
	{
		dp(des[k],k);
		if (w[k]>w[e]) ans=1LL*ans*nifac[size[des[k]]]%mo*f[des[k]]%mo;
		else f[s]=1LL*f[s]*f[des[k]]%mo*C(size[s]+=size[des[k]],size[des[k]])%mo;
	}
	size[s]++;
}
int main()
{
	tt=1;scanf("%d",&n);n<<=1;
	fac[0]=nifac[0]=1;
	for (int i=1;i<=n;i++) {fac[i]=fac[i-1]*i%mo;nifac[i]=getmi(fac[i],mo-2);}
	for (int i=1;i<=n;i++)
	{
		int u,v;scanf("%d%d",&u,&v);v+=n/2;
		des[++tt]=v;next[tt]=first[u];first[u]=tt;
		des[++tt]=u;next[tt]=first[v];first[v]=tt;
		w[tt]=w[tt^1]=u+v;
	}
 
	int all=fac[n];
	for (int s=1;s<=n;s++) if (!vis[s])
	{
		vn=en=ed=0;dfs(s,0);en>>=1;
		if (vn!=en) {puts("0");return 0;}
		int u=des[ed],v=des[ed^1];
		for (;u!=v;u=des[e[u]]) if (w[e[u]]>w[ed]) ed=e[u];
		ans=1;dp(des[ed],ed);
		int p1=(long long)ans*nifac[size[des[ed]]]%mo*f[des[ed]]%mo;
		ans=1;dp(des[ed^1],ed);
		int p2=(long long)ans*nifac[size[des[ed^1]]]%mo*f[des[ed^1]]%mo;
		all=(long long)all*(p1+p2)%mo;
	}
	printf("%d\n",all);
}

Compiler output:
a.cc: In function 'void dfs(int, int)':
a.cc:24:33: error: reference to 'next' is ambiguous
   24 |         for (int k=first[s];k;k=next[k])
      |                                 ^~~~
In file included from /usr/include/c++/14/bits/stl_algobase.h:66,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:5:19: note:                 'int next [100010]'
    5 | int first[100010],next[100010],des[100010],w[100010];
      |                   ^~~~
a.cc: In function 'void dp(int, int)':
a.cc:33:16: error: reference to 'size' is ambiguous
   33 |         f[s]=1;size[s]=0;
      |                ^~~~
In file included from /usr/include/c++/14/string:53,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52:
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:6:5: note:                 'int size [100010]'
    6 | int size[100010],ans,f[100010],vn,en,ed;
      |     ^~~~
a.cc:34:33: error: reference to 'next' is ambiguous
   34 |         for (int k=first[s];k;k=next[k]) if (k/2!=ed/2&&(k^1^e))
      |                                 ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:5:19: note:                 'int next [100010]'
    5 | int first[100010],next[100010],des[100010],w[100010];
      |                   ^~~~
a.cc:37:50: error: reference to 'size' is ambiguous
   37 |                 if (w[k]>w[e]) ans=1LL*ans*nifac[size[des[k]]]%mo*f[des[k]]%mo;
      |                                                  ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:6:5: note:                 'int size [100010]'
    6 | int size[100010],ans,f[100010],vn,en,ed;
      |     ^~~~
a.cc:38:51: error: reference to 'size' is ambiguous
   38 |                 else f[s]=1LL*f[s]*f[des[k]]%mo*C(size[s]+=size[des[k]],size[des[k]])%mo;
      |                                                   ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:6:5: note:                 'int size [100010]'
    6 | int size[100010],ans,f[100010],vn,en,ed;
      |     ^~~~
a.cc:38:60: error: reference to 'size' is ambiguous
   38 |                 else f[s]=1LL*f[s]*f[des[k]]%mo*C(size[s]+=size[des[k]],size[des[k]])%mo;
      |                                                            ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:6:5: note:                 'int size [100010]'
    6 | int size[100010],ans,f[100010],vn,en,ed;
      |     ^~~~
a.cc:38:73: error: reference to 'size' is ambiguous
   38 |                 else f[s]=1LL*f[s]*f[des[k]]%mo*C(size[s]+=size[des[k]],size[des[k]])%mo;
      |                                                                         ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:6:5: note:                 'int size [100010]'
    6 | int size[100010],ans,f[100010],vn,en,ed;
      |     ^~~~
a.cc:40:9: error: reference to 'size' is ambiguous
   40 |         size[s]++;
      |         ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:6:5: note:                 'int size [100010]'
    6 | int size[100010],ans,f[100010],vn,en,ed;
      |     ^~~~
a.cc: In function 'int main()':
a.cc:50:29: error: reference to 'next' is ambiguous
   50 |                 des[++tt]=v;next[tt]=first[u];first[u]=tt;
      |                             ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:5:19: note:                 'int next [100010]'
    5 | int first[100010],next[100010],des[100010],w[100010];
      |                   ^~~~
a.cc:51:29: error: reference to 'next' is ambiguous
   51 |                 des[++tt]=u;next[tt]=first[v];first[v]=tt;
      |                             ^~~~
/usr/include/c++/14/bits/stl_iterator_base_funcs.h:232:5: note: candidates are: 'template<class _InputIterator> constexpr _InputIterator std::next(_InputIterator, typename iterator_traits<_Iter>::difference_type)'
  232 |     next(_InputIterator __x, typename
      |     ^~~~
a.cc:5:19: note:                 'int next [100010]'
    5 | int first[100010],next[100010],des[100010],w[100010];
      |                   ^~~~
a.cc:63:45: error: reference to 'size' is ambiguous
   63 |                 int p1=(long long)ans*nifac[size[des[ed]]]%mo*f[des[ed]]%mo;
      |                                             ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:6:5: note:                 'int size [100010]'
    6 | int size[100010],ans,f[100010],vn,en,ed;
      |     ^~~~
a.cc:65:45: error: reference to 'size' is ambiguous
   65 |                 int p2=(long long)ans*nifac[size[des[ed^1]]]%mo*f[des[ed^1]]%mo;
      |                                             ^~~~
/usr/include/c++/14/bits/range_access.h:272:5: note: candidates are: 'template<class _Tp, long unsigned int _Nm> constexpr std::size_t std::size(const _Tp (&)[_Nm])'
  272 |     size(const _Tp (&)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:262:5: note:                 'template<class _Container> constexpr decltype (__cont.size()) std::size(const _Container&)'
  262 |     size(const _Container& __cont) noexcept(noexcept(__cont.size()))
      |     ^~~~
a.cc:6:5: note:                 'int size [100010]'
    6 | int size[100010],ans,f[100010],vn,en,ed;
      |     ^~~~