Problem:
Score : 1400 points

Problem Statement

You are given an integer sequence a of length N.
How many permutations p of the integers 1 through N satisfy the following condition?

For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.

Find the count modulo 10^9 + 7.

Constraints

1 ≤ N ≤ 10^5

a_i is an integer.

1 ≤ a_i ≤ N

Input

The input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.

Sample Input 1

3
1 2 3

Sample Output 1

4

The following four permutations satisfy the condition:

(1, 2, 3)

(1, 3, 2)

(3, 2, 1)

(2, 1, 3)

For example, (1, 3, 2) satisfies the condition because p_1 = 1, p_{p_2} = 2 and p_{p_3} = 3.

Sample Input 2

2
1 1

Sample Output 2

1

The following one permutation satisfies the condition:

(2, 1)

Sample Input 3

3
2 1 1

Sample Output 3

2

The following two permutations satisfy the condition:

(2, 3, 1)

(3, 1, 2)

Sample Input 4

3
1 1 1

Sample Output 4

0

Sample Input 5

13
2 1 4 3 6 7 5 9 10 8 8 9 11

Sample Output 5

6

Solution in C++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#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; }
}
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];
				}
				rev(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;
}