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<cstring>
#include<algorithm>
#define MAXN 100000
#define MO 1000000007
using namespace std;
int N,a[MAXN+5],d[MAXN+5],pre[MAXN+5];
int cnt[MAXN+5],f[MAXN+5];
bool vis[MAXN+5];
int main()
{
//	freopen("test.in","r",stdin);
//	freopen("test.out","w",stdout);
	scanf("%d",&N);
	for(int i=1;i<=N;i++)
		scanf("%d",&a[i]),d[a[i]]++;
	for(int i=1;i<=N;i++)
		if(d[i]>2)
		{
			printf("0\n");
			return 0;
		}
	for(int i=1;i<=N;i++)
		if(d[i]==2&&vis[i]==false)
		{
			int p=i;
			do
			{
				if(vis[p]==true)
				{
					printf("0\n");
					return 0;
				}
				vis[p]=true;
				pre[a[p]]=p;
				p=a[p];
			}while(p!=i);
		}
	int ans=1;
	for(int i=1;i<=N;i++)//处理基环内向树 
		if(d[i]==0)
		{
			int p=i,len1=0,len2=0;
			do
			{
				vis[p]=true;
				p=a[p];
				len1++;
			}while(vis[p]==false);
			do
			{
				p=pre[p];
				len2++;
			}while(d[p]==1);
			if(len1>len2)
			{
				printf("0\n");
				return 0;
			}
			if(len2>len1)
				ans=2LL*ans%MO;
		}
	for(int i=1;i<=N;i++)//处理单个的循环 
		if(vis[i]==false)
		{
			int len=0,p=i;
			do
			{
				p=a[p];
				vis[p]=true;
				len++;
			}while(p!=i);
			cnt[len]++;
		}
	for(int i=1;i<=N;i++)
		if(cnt[i])
		{
			int mul=1;
			if(i%2==1&&i!=1)
				mul=2;
			f[0]=1,f[1]=mul;
			for(int j=2;j<=cnt[i];j++)
				f[j]=(1LL*f[j-2]*(j-1)%MO*i%MO+1LL*f[j-1]*mul%MO)%MO;
			ans=1LL*ans*f[cnt[i]]%MO;
		}
	printf("%d\n",ans);
	return 0;
}