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: Accepted)

#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int N=100100,P=1000000007;
int i,j,k,n,m,ch,ff,ans;
int a[N],b[N],z[N],fg[N],num[N],f[N],Jc[N],Jc_[N],tm2[N],d[N],dp[N];
void R(int &x) {
	ff=x=0;ch=getchar();
	while (ch<'0' || '9'<ch) { if (ch=='-') ff=1;ch=getchar();}
	while ('0'<=ch && ch<='9') x=x*10+ch-'0',ch=getchar();
	if (ff) x=-x;
}
int ksm(int x,int y) {
	int z=1;
	for (;y;y>>=1,x=(ll) x*x%P) if (y&1) z=(ll) z*x%P;
	return z;
}
void pre(int n) {
	int i;
	Jc[0]=Jc_[0]=f[0]=tm2[0]=1;
	for (i=1;i<=n;i++) tm2[i]=(tm2[i-1]+tm2[i-1])%P;
	for (i=1;i<=n;i++) f[i]=(ll) f[i-1]*(2*i-1)%P;
	for (i=1;i<=n;i++) Jc[i]=(ll) Jc[i-1]*i%P;
	Jc_[n]=ksm(Jc[n],P-2);
	for (i=n-1;i;i--) Jc_[i]=(ll) Jc_[i+1]*(i+1)%P;
}
int C(int n,int m) {
	return (ll) Jc[n]*Jc_[m]%P*Jc_[n-m]%P;
}
int Js(int n,int m) {
	int i,k,ans=0;
	if ((m&1) && m>1) {
		for (i=0,k=1;i+i<=n;i++,k=(ll) k*m%P) ans=((ll) f[i]*k%P*C(n,i+i)%P*tm2[n-i-i]%P+ans)%P;
	}
	else {
		for (i=0,k=1;i+i<=n;i++,k=(ll) k*m%P) ans=((ll) f[i]*k%P*C(n,i+i)%P+ans)%P;
	}
	return ans;
}
int main() {
	R(n);
	pre(n);
	for (i=1;i<=n;i++) R(a[i]);
	for (i=1;i<=n;i++) d[a[i]]++;
	for (i=1;i<=n;i++) if (!z[i]) {
		for (j=i;z[j]!=i && !z[j];j=a[j]) z[j]=i;
		if (z[j]==i) {
			fg[j]=1;
			for (k=a[j];k!=j;k=a[k]) fg[k]=1;
		}
	}
	for (i=1;i<=n;i++) if (d[i]>fg[i]+1) return puts("0"),0;
	for (i=1;i<=n;i++) if (!fg[i]) b[a[i]]=i;
	for (i=1;i<=n;i++) if (fg[i]) {
		for (j=b[i];j;j=b[j]) dp[i]++;
	}
	memset(z,0,sizeof z);
	ans=1;
	for (i=1;i<=n;i++) if (!z[i] && fg[i]) { 
		int nm=0,Fg=0;
		for (j=i;!z[j];j=a[j]) {
			z[j]=1;
			nm++;
			if (dp[j]) Fg=j;
		}
		if (!Fg) num[nm]++;
		else {
			k=0;
			for (j=a[Fg];j!=Fg;j=a[j]) {
				k++;
				if (dp[j]) {
					if (dp[j]>k) ans=0;
					if (dp[j]<k) ans=(ans+ans)%P;
					k=0;
				}
			}
			k++;
			if (dp[Fg]>k) ans=0;
			if (dp[Fg]<k) ans=(ans+ans)%P;
		}
	}
	for (i=1;i<=n;i++) if (num[i]) ans=(ll) ans*Js(num[i],i)%P;
	printf("%d\n",ans);
}