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 <algorithm>
#include <vector>
const int MOD = 1000000007;
int N, a[100001], v[100001], bel[100001], dep[100001], pure[100001], O = 1;
std::vector < int > V[100001];
std::vector < std::vector < int > > cirs;
int main()
{
	scanf("%d", &N);
	for (int i = 1; i <= N; i++)
		scanf("%d", a + i);
	for (int i = 1; i <= N; i++)
		if (!v[i])
		{
			static int way[100001];
			int L = 1;
			v[way[L] = i] = 1;
			while (!v[a[way[L]]])
			{
				v[way[L + 1] = a[way[L]]] = 1;
				L++;
			}
			way[L + 1] = a[way[L]];
			if (v[a[way[L]]] == 2)
				for (int i = L; i; i--)
				{
					bel[way[i]] = bel[way[i + 1]];
					dep[way[i]] = dep[way[i + 1]] + 1;
				}
			else
			{
				int S = std::find(way + 1, way + L + 1, way[L + 1]) - way;
				for (int i = S; i <= L; i++)
				{
					bel[way[i]] = way[i];
					dep[way[i]] = 0;
				}
				for (int i = S - 1; i; i--)
				{
					bel[way[i]] = bel[way[i + 1]];
					dep[way[i]] = dep[way[i + 1]] + 1;
				}
				cirs.push_back({ way + S, way + L + 1 });
			}
			for (int i = 1; i <= L; i++)
				v[way[i]] = 2;
		}
	for (int i = 1; i <= N; i++)
		V[bel[i]].push_back(dep[i]);
	for (int i = 1; i <= N; i++)
	{
		std::sort(V[i].begin(), V[i].end());
		int S = V[i].size();
		for (int j = 0; j < S; j++)
			if (V[i][j] != j)
			{
				puts("0");
				return 0;
			}
	}
	for (auto &cir : cirs)
	{
		for (int &i : cir)
			i = V[i].size() - 1;
		if (int(std::count(cir.begin(), cir.end(), 0)) == int(cir.size()))
			pure[cir.size()]++;
		else
		{
			int *A = cir.data(), L = cir.size();
			for (int i = 0; i < L; i++)
				if (A[i])
				{
					for (int j = 1; j < A[i]; j++)
						if (A[(i - j % L + L) % L])
						{
							puts("0");
							return 0;
						}
					if (!A[(i - A[i] % L + L) % L])
						O = (O + O) % MOD;
				}
		}
	}
	for (int i = 1; i <= N; i++)
	{
		static int f[100001];
		f[0] = 1;
		for (int j = 1; j <= pure[i]; j++)
			f[j] = (f[j - 1] * (i > 1 && (i & 1) ? 2 : 1) + (long long)f[j - 2] * (j - 1) * i) % MOD;
		O = (long long)O * f[pure[i]] % MOD;
	}
	printf("%d\n", O);
	return 0;
}