Language: C++

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<set>
#include<queue>
#include<stack>
#include<bitset>
using namespace std;
int p = 1000000007;
#define int long long
#define vel vector<long long>
#define vvel vector<vel>
#define rep(i,n) for(long long i=0;i<n;i++)
#define sor(v) sort(v.begin(),v.end())
#define mmax(a,b) a=max(a,b)
#define mmin(a,b) a=min(a,b)
#define mkp make_pair
#define pin pair<int,int>
#define V vector
#define Endl endl
#define veb vector<bool>
#define sq(a) (a)*(a)
#define rev(s) reverse(s.begin(),s.end())
#define end_program(s) cout << s <<endl;return 0
int kai_size = 120001;
vel kai(kai_size, 1);
vel ink(kai_size, 1);
vel dist;
int RE() {
	vel v(3, 2);
	return v.at(4); 
}
int ru(int a, int r) {
	if (r == 0) { return 1; }
	int ans = ru(a, r / 2);
	ans *= ans; ans %= p;
	if (r % 2 == 1) { ans *= a; }
	return ans % p;
}
int inv(int a) {
	return ru(a, p - 2);
}
void make_kai() {
	rep(i, kai_size-1) { kai[i + 1] = (kai[i] * (i + 1)) % p; }
	rep(i, kai_size) { ink[i] = inv(kai[i]); }
}
void make_true(vel &a, veb &smt) {
	for (int x : a) { smt[x] = true; }
}
void make_false(vel &a, veb &smt) {
	for (int x : a) { smt[x] = false; }
}
int com(int n, int r) {
	int ans = kai[n] * ink[r];
	ans %= p; ans *= ink[n - r]; ans %= p;
	return ans;
}
int per(int n, int r) {
	int ans = kai[n] * ink[n - r];
	ans %= p;
	return ans;
}
vel dis(int mid1, vvel &way) {
	int n = way.size();
	vel dist(n, -1); dist[mid1] = 0;
	queue<int> q;
	q.push(mid1);
	while (!q.empty()) {
		int st = q.front(); q.pop();
		rep(i, way[st].size()) {
			int to = way[st][i];
			if (dist[to] == -1) {
				dist[to] = dist[st] + 1;
				q.push(to);
			}
		}
	}
	return dist;
}
vel conected(int st, vvel &way,veb &not_seen) {
	int n = way.size();
	not_seen[st] = false;
	vel ans(0);
	queue<int> q;
	q.push(st);
	while (!q.empty()) {
		int st1 = q.front(); q.pop();
		ans.push_back(st1);
		rep(i, way[st1].size()) {
			int to = way[st1][i];
			if (not_seen[to]) {
				not_seen[to] = false;
				q.push(to);
			}
		}
	}
	make_true(ans, not_seen);
	return ans;
}
pin most_far(int now, int n, vvel &way) {
	vel dist1 = dis(now, way);
	pin ans = mkp(-1, 0);
	rep(i, n) {
		if (dist1[i] > ans.first) { ans = mkp(dist1[i], i); }
	}
	return ans;
}
vel find_cycle(int st,vel &a,veb &smt) {
	vel root(0);
	while (smt[st]) {
		root.push_back(st);
		smt[st] = false;
		st = a[st];
	}
	vel ans(0);
	bool fl = false;
	for (int x : root) {
		if (x == st) { fl = true; }
		if (fl) { ans.push_back(x); }
	}
	make_true(root, smt);
	return ans;
}
int div(int i) {
	if (i % 2 == 0) { return i/2; }
	else { return (i + p) / 2; }
}
vel pus(vel v) {
	vel w = v;
	int n = v.size();
	rep(i, n) {
		w[(i + 1) % n] = v[i];
	}
	return w;
}
signed main() {
	int n; cin >> n;
	vel a(n);
	vvel inv_a(n);
	vvel way(n);
	rep(i, n) { 
		cin >> a[i]; a[i]--; way[i].push_back(a[i]);
		inv_a[a[i]].push_back(i);
		way[a[i]].push_back(i);
	}
	veb seen(n, false); 
	veb smt(n, true);
	vel cnt_cy(n+1, 0);
	int ans = 1;
	rep(i, n) {
		if (!seen[i]) {
			vel cone = conected(i, way,smt);
			vel cy=find_cycle(i, a,smt);
			int count_3;
			make_false(cy, smt);
			for (auto x : cone) {
				seen[x] = true; 
				if (smt[x]) { 
					if (inv_a[x].size() > 1) { end_program(0); }
				}
				else {
					if (inv_a[x].size() > 2) { end_program(0); }
				}
			}
			if (cone.size() == cy.size()) {cnt_cy[cone.size()]++;}
			else {
				int mi = i;
				while (inv_a[mi].size()!=2) { mi = a[mi]; }
				vel count0;
				vel count1;
				int bk = -1;
				for (auto x : inv_a[mi]) { if (smt[x]) { bk = x; } }
				int counter = 1;
				while (inv_a.at(bk).size() == 1) { counter++; bk = inv_a[bk][0]; }
				count0.push_back(counter);
				counter = 0;
				int now = a[mi];
				while (now!=mi) {
					if (way[now].size() == 3) {
						count1.push_back(counter);
						counter = 1;
						for (auto x : inv_a[now]) { if (smt[x]) { bk = x; } }
						while (inv_a[bk].size() == 1) { counter++; bk = inv_a[bk][0]; }
						count0.push_back(counter);
						counter = 0;
					}
					else {
						counter++;
					}
					now = a[now];
				}
				push(count0);
				count1.push_back(counter);
				rep(j, count0.size()) {
					int mul = count1[j] + 2 - count0[j];
					if (mul <= 0) { end_program(0); }
					int two = 2;
					ans *= min(mul,two); ans %= p;
				}
			}
			make_true(cy, smt);
		}
	}
	make_kai();
	for (int i = 1; i <= n; i++) {
		int mul = 0;
		int cys = cnt_cy[i];
		if (cys != 0) {
			for (int j = 0; j * 2 <= cys; j++) {
				int pl = per(cys, 2 * j);
				pl *= ink[j]; pl %= p;
				pl *= ru(div(i), j); pl %= p;
				if (i % 2 == 1 and i != 1) {
					pl *= ru(2, cys - 2 * j); pl %= p;
				}
				mul += pl;
			}
			mul %= p;
			ans *= mul; ans %= p;
		}
	}
	cout << ans << endl;
	return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:193:33: error: 'push' was not declared in this scope; did you mean 'pus'?
  193 |                                 push(count0);
      |                                 ^~~~
      |                                 pus