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: Wrong Answer)

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

const int maxn(1e5 + 5);
const int mod(1e9 + 7);

inline void Inc(int &x, int y) {
    x = x + y >= mod ? x + y - mod : x + y;
}

inline void Dec(int &x, int y) {
    x = x - y < 0 ? x - y + mod : x - y;
}

inline int Add(int x, int y) {
    return x + y >= mod ? x + y - mod : x + y;
}

inline int Sub(int x, int y) {
    return x - y < 0 ? x - y + mod : x - y;
}

int n, cir[maxn], cnt, fa[maxn], vis[maxn], len, d[maxn], in[maxn];
int ans, f[maxn], a[maxn], chain[maxn], que[maxn << 1];

inline void GetCircle() {
	int i;
	for (i = 1; i <= len; ++i) if (d[que[i]] ^ 1) return;
	cir[++cnt] = len;
	for (i = 1; i <= len; ++i) vis[que[i]] = 2;
}

void Dfs1(int u) {
	int cur;
	vis[u] = 1, in[u] = 1;
	if (!vis[a[u]]) fa[a[u]] = u, Dfs1(a[u]);
	else if (in[a[u]]) {
		len = 0;
		for (cur = u; ; cur = fa[cur]) {
			que[++len] = cur, vis[cur] = 3;
			if (cur == a[u]) break;
		}
		GetCircle();
	}
	in[u] = 0;
}

void Dfs2(int u) {
	chain[a[u]] = chain[u] + 1;
	if (vis[a[u]] > 1) return;
	Dfs2(a[u]);
}

int Solve(int x) {
	int cur, i, j, ret = 1;
	que[len = 1] = x;
	for (cur = a[x]; cur ^ x; cur = a[cur]) que[++len] = cur;
	reverse(que + 1, que + len + 1), cur = len + len;
	for (i = 1; i <= len; ++i) vis[que[i]] = 4, que[len + i] = que[i];
	for (i = 1, j = 2; i <= len; i = j + 1)
		if (chain[que[i]]) {
			for (j = max(j, i + 1); j <= cur && !chain[que[j]]; ++j);
			if (chain[que[i]] > j - i) puts("0"), exit(0);
			if (chain[que[i]] < j - i) ret = Add(ret, ret);
		}
	return ret;
}

int main() {
	int i, j, k, ret = 1;
	scanf("%d", &n);
	for (i = 1; i <= n; ++i) scanf("%d", &a[i]), ++d[a[i]];
	for (i = 1; i <= n; ++i) if (!d[i]) Dfs1(i);
	for (i = 1; i <= n; ++i) if (!vis[i]) Dfs1(i);
	for (i = 1; i <= n; ++i)
		if ((vis[i] > 1 && d[i] > 2) || (vis[i] == 1 && d[i] > 1)) return puts("0"), 0;
	sort(cir + 1, cir + cnt + 1);
	for (i = 1; i <= n; i = j) {
		for (j = i; j <= n && cir[j] == cir[i]; ++j);
		f[i - 1] = 1;
		for (k = i; k < j; ++k) {
			f[k] = f[k - 1];
			if (cir[i] > 1 && (cir[i] & 1)) Inc(f[k], f[k - 1]);
			if (k > i) Inc(f[k], (ll)f[k - 2] * (k - i) % mod * cir[i] % mod);
		}
		ret = (ll)ret * f[j - 1] % mod;
	}
	for (i = 1; i <= n; ++i) if (vis[i] == 1) vis[i] = 0;
	for (i = 1; i <= n; ++i) if (!d[i]) Dfs2(i);
	for (i = 1; i <= n; ++i) if (vis[i] == 3) ret = (ll)ret * Solve(i) % mod;
	printf("%d\n", ret);
	return 0;
}