Language: C++

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

#define FOR(i,a,b) for(int i = (a); i < (b); i++)
#define RFOR(i,b,a) for(int i = (b) - 1; i >= (a); i--)
#define ITER(it, a) for(typeof(a.begin()) it = a.begin(); it != a.end(); it++)
#define FILL(a, value) memset(a, value, sizeof(a))

#define SZ(a) (int) a.size()
#define ALL(a) a.begin(),a.end()
#define PB push_back
#define MP make_pair

typedef long long LL;
typedef vector<int> VI;
typedef pair<int, int> PII;

const double PI = acos(-1.0);
const LL INF = 1000 * 1000 * 1000 + 7;
const LL LINF = INF * (LL)INF;

const int MAX = 200200;
const int MOD = 1000 * 1000 * 1000 + 7;

VI g[MAX];
VI gr[MAX];
int U[MAX];
int iter;

int L[MAX];
int S[MAX];
int CNT[MAX];

LL DP[MAX];

int dfs1(int x)
{
	U[x] = iter;
	int to = g[x][0];
	if (U[to] == iter) return to;
	return dfs1(to);
}

int dfsC(int x)
{
	U[x] = iter;
	int res = 1;
	FOR (i, 0, SZ(gr[x]))
	{
		int to = gr[x][i];
		if (U[to] == iter) continue;
		res += dfsC(to);
	}
	return res;
}

int dfsLen(int x)
{
	if (SZ(gr[x]) > 1) return -1;

	if (SZ(gr[x]) == 0) return 1;

	int res = dfsLen(gr[x][0]);
	if (res == -1) return -1;
	return res + 1;
}

int sum(int l, int r)
{
	if (l > r) return 0;
	int res = S[r];
	if (l) res -= S[l-1];
	return res;
}

int brute(VI a)
{
	VI v;
	FOR (i, 0, SZ(a))
	{
		v.PB(i);
	}

	int res = 0;

	do
	{
		bool ok = true;
		FOR (i, 0, SZ(v))
		{
			if (v[i] != a[i] && v[v[i]] != a[i])
			{
				ok = false;
				break;
			}
		}

		if (ok)
		{
			res++;
		}

	}while(next_permutation(ALL(v)));

	return res;
}

LL go(int x)
{
	iter++;
	x = dfs1(x);

	VI cycle;
	iter++;
	while(U[x] != iter)
	{
		U[x] = iter;
		cycle.PB(x);
		x = g[x][0];
	}

	reverse(ALL(cycle));

	iter++;
	int sz = dfsC(x);

	//cout<<x<<endl;

	//cout<<sz<<' '<<SZ(cycle)<<endl;

	if (sz == SZ(cycle))
	{
		CNT[sz]++;
		return 1;
	}

	if (sz == 3)
	{
		int x = cycle[0];
	//	cout<<"!! "<<x<<' '<<SZ(gr[x])<<endl;
		if (SZ(gr[x]) == 3) return 0;
		if (g[x][0] == x) return 0;
		return 2;
	}

	if (sz == 2)
	{
		CNT[2]++;
		return 1;
	}
	//cout<<"---"<<endl;

	iter++;
	FOR (i, 0, SZ(cycle))
	{
		U[cycle[i]] = iter;
	}


	FOR (i, 0, SZ(cycle))
	{
		int x = cycle[i];
		if (SZ(gr[x]) == 1)
		{
			L[i] = 0;
			continue;
		}

		if (SZ(gr[x]) > 2)
		{
			return 0;
		}

		int to = gr[x][0];
		if (U[to] == iter) to = gr[x][1];
		int len = dfsLen(to);

		L[i] = len;
	}

	FOR (i, SZ(cycle), SZ(cycle) * 2 + 2)
	{
		L[i] = L[i - SZ(cycle)];
	}

	FOR (i, 0, SZ(cycle) * 2 + 2)
	{
		S[i] = L[i];
		if (i) S[i] += S[i-1];
	}

	FOR (i, 0, SZ(cycle) * 2 + 2)
	{
		DP[i] = 0;
	}

	DP[0] = 1;

/*	FOR (i, 0, SZ(cycle))
	{
		cout<<cycle[i]<<' '<<L[i]<<endl;
	}
	cout<<endl;*/

	FOR (i, 0, SZ(cycle))
	{
		int len = L[i];
		if (len == 0)
		{
			DP[i+1] = (DP[i+1] + DP[i]);
			if (DP[i+1] >= MOD) DP[i+1] -= MOD;
			continue;
		}

		int to = i + len;

		if (to > SZ(cycle) * 2 + 1) continue;

		if (sum(i+1, to - 1) == 0)
		{
			DP[to] += DP[i];
			if (DP[to] >= MOD) DP[to] -= MOD;
		}

		to = i + len + 1;
		if (to > SZ(cycle) * 2 + 1) continue;

		if (sum(i+1, to - 1) == 0)
		{
			DP[to] += DP[i];
			if (DP[to] >= MOD) DP[to] -= MOD;
		}
	}

/*	FOR (i,0 , SZ(cycle) * 2 + 2)
	{
		cout<<DP[i]<<' ';
	}
	cout<<endl;*/

	LL res = 0;
	FOR (i, SZ(cycle), SZ(cycle) * 2 + 2)
	{
		res += DP[i];
		if (res >= MOD) res -= MOD;
	}

	return res;
}

LL bpow(LL a, LL b)
{
	LL res = 1;
	while(b)
	{
		if (b & 1) res = (res * a) % MOD;
		a = (a * a) % MOD;
		b /= 2;
	}
	return res;
}

LL inv(LL a)
{
	return bpow(a, MOD - 2);
}

LL F[MAX];
LL IF[MAX];
LL P2[MAX];
LL IP2[MAX];
LL V[MAX];

LL V2[MAX];

LL C(int n, int m)
{
	if (m > n) return 0;

	LL res = F[n];
	res *= IF[m];
	res %= MOD;
	res *= IF[n-m];
	res %= MOD;
	return res;
}


int main()
{
	//freopen("in.txt", "r", stdin);
	//ios::sync_with_Stdio(false); cin.tie(0);

	F[0] = 1;
	IF[0] = 1;
	P2[0] = 1;
	IP2[0] = 1;

	V[0] = 1;
	V2[0] = 1;

	FOR (i, 1, MAX)
	{
		if (i % 2 == 0)
		{
			V[i] = V[i-2] * (i-1);
			V[i] %= MOD;
		}

		if (i % 3 == 0)
		{
			V[i] = V[i-3] * (i-1);
			if ((i - 1) % 2 == 0) V[i] /= 2;
			V[i] %= MOD;
			V[i] *= (i-2);
			if ((i-2) % 2) == 0) V[i] /= 2;
			V[i] %= MOD;
		}
		F[i] = (F[i-1] * i) % MOD;
		IF[i] = inv(F[i]);
		P2[i] = (P2[i-1] * 2) % MOD;
		IP2[i] = inv(P2[i]);
	}

	while(true)
	{
		//cout<<"---------"<<endl;
		int n;
		scanf("%d", &n);
		//n = 8;

		FOR (i, 0, n+2)
		{
			U[i] = 0;
			g[i].clear();
			gr[i].clear();
			CNT[i] = 0;
		}
		iter = 0;

		VI a;

		FOR (i, 0, n)
		{
			int x;
			scanf("%d", &x);
			x--;
			//x = rand() % n;

			a.PB(x);

			g[i].PB(x);
			gr[x].PB(i);
		}

		LL res = 1;

		FOR (i, 0, n)
		{
			if (U[i]) continue;

			res *= go(i);
			res %= MOD;
		//	cout<<"* "<<res<<endl;
		}

		FOR (i, 0, n+1)
		{
			if (CNT[i] == 0) continue;

			LL mul = 0;

			if (i == 1)
			{
				FOR (j, 0, CNT[i] + 1)
				{
					if (j * 3 > CNT[i]) continue;

					LL cur = C(CNT[i], j*3);

					/*cur *= F[j*2];
					cur %= MOD;
					cur *= IP2[j];
					cur %= MOD;*/

					cur *= V2[j*3];
					cur %= MOD;

					cur *= bpow(i, j);
					cur %= MOD;

					if (i > 1 && (i & 1))
					{
						cur *= P2[CNT[i] - 3 * j];
						cur %= MOD;
					}

				//	cout<<j<<' '<<cur<<endl;

					mul += cur;
					mul %= MOD;
				}
			}

			FOR (j, 0, CNT[i] + 1)
			{
				if (j * 2 > CNT[i]) continue;

				LL cur = C(CNT[i], j*2);

				/*cur *= F[j*2];
				cur %= MOD;
				cur *= IP2[j];
				cur %= MOD;*/

				cur *= V[j*2];
				cur %= MOD;

				cur *= bpow(i, j);
				cur %= MOD;

				if (i > 1 && (i & 1))
				{
					cur *= P2[CNT[i] - 2 * j];
					cur %= MOD;
				}

			//	cout<<j<<' '<<cur<<endl;

				mul += cur;
				mul %= MOD;
			}

			res *= mul;
			res %= MOD;
		}
		cout<<res<<endl;
	/*	int br = brute(a);
		cout<<br<<endl;
		if (res != br)
		{
			cout<<SZ(a)<<endl;
			FOR (i, 0, SZ(a))
			{
				cout<<a[i]<<' ';
			}
			cout<<endl;
			throw -1;
		}
		cout<<endl;*/
		break;

	}

}


Compiler output:
a.cc: In function 'int main()':
a.cc:316:40: error: expected primary-expression before '==' token
  316 |                         if ((i-2) % 2) == 0) V[i] /= 2;
      |                                        ^~