Problem: 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.

Examples

Input

3
1 2 3


Output

4


Input

2
1 1


Output

1


Input

3
2 1 1


Output

2


Input

3
1 1 1


Output

0


Input

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


Output

6

Solution (in CPP):

Solution:
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

#define iter(i, n) for (int i = 1; i <= n; ++i)
#define NR 101000

const int mod = 1e9 + 7;

int n, a[NR], in[NR], in_x[NR], cnt[NR], f[NR], cc[NR], sz;
bool vis[NR], ring[NR];
vector<int> R[NR];

int main() {
	scanf("%d", &n);
	iter(i, n) {
		scanf("%d", &a[i]);
		++in[a[i]];
		in_x[a[i]] = i;
	}
	iter(i, n) if (in[i] > 2) { puts("0"); return 0; }
	int ans = 1;
	iter(i, n) if (!vis[i]) {
		int x = i; bool is_ring = true;
		for (; !vis[x]; x = a[x]) {
			vis[x] = true;
		}

		if (!ring[x]) {
			++sz;
			for (; !ring[x]; x = a[x]) {
				if (in[x] != 1) is_ring = false;
				ring[x] = true;
				R[sz].push_back(x);
			}
		}

		if (x != i) {
			bool is_chain = true;
			x = i;
			while (in[x] == 1) x = in_x[x];
			if (in[x] != 0) { puts("0"); return 0; }
			int len = 1;
			for (vis[x] = true, x = a[x]; !ring[x]; x = a[x]) {
				++len;
				vis[x] = true;
				if (in[x] != 1) { puts("0"); return 0; }
			}
			cc[x] = len;
		} else {
			if (is_ring) ++cnt[R[sz].size()];
		}
	}

	iter(i, sz) {
		reverse(R[i].begin(), R[i].end());
		int rem = 0;
		for (int x : R[i]) {
			if (cc[x] != 0) {
				if (rem > 1) { puts("0"); return 0; }
				else rem = cc[x];
			} else if (rem != 0) {
				--rem;
				if (rem == 0) ans = ans * 2 % mod;
			}	
		}

		if (rem != 0) {
			for (int x : R[i]) {
				if (cc[x] != 0) {
					if (rem > 1) { puts("0"); return 0; }
					else break;
				} else {
					--rem;
					if (rem == 0) {
						ans = ans * 2 % mod;
						break;
					}
				}
			}
		}
	}

	iter(k, n) {
		f[0] = 1;
		iter(i, cnt[k]) {
			f[i] = f[i - 1] * ((k % 2 && k != 1) ? 2 : 1) % mod;
			if (i > 1) f[i] = (f[i] + 1ll * (i - 1) * f[i - 2] % mod * k) % mod;
		}
		ans = 1ll * ans * f[cnt[k]] % mod;
	}
	printf("%d\n", ans);
	return 0;
}