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<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define N 100010
#define gc getchar()
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define lint long long
#define mod 1000000007
#define debug(x) cerr<<#x<<"="<<x
#define sp <<" "
#define ln <<endl
using namespace std;
inline int inn()
{
	int x,ch;while((ch=gc)<'0'||ch>'9');
	x=ch^'0';while((ch=gc)>='0'&&ch<='9')
		x=(x<<1)+(x<<3)+(ch^'0');return x;
}
int fac[N],facinv[N],f[N],onc[N],sz[N],p[N],cnt[N];
int cyc_cnt,vis[N],l[N],pre[N],jhs[N],in[N],mi[N],it[N];
inline int fast_pow(int x,int k,int ans=1)
{	for(;k;k>>=1,x=(lint)x*x%mod) (k&1)?ans=(lint)ans*x%mod:0;return ans;	}
inline int prelude(int n)
{
	rep(i,fac[0]=1,n) fac[i]=(lint)fac[i-1]*i%mod;
	facinv[n]=fast_pow(fac[n],mod-2);
	for(int i=n-1;i>=0;i--) facinv[i]=facinv[i+1]*(i+1ll)%mod;
	rep(i,mi[0]=1,n) mi[i]=mi[i-1]*2,(mi[i]>=mod?mi[i]-=mod:0);
	rep(i,f[0]=1,n/2) f[i]=f[i-1]*(2*i-1ll)%mod;return 0;
}
inline int C(int n,int m)
{
	if(n<0||m<0||n<m) return 0;
	return (lint)fac[n]*facinv[m]%mod*facinv[n-m]%mod;
}
int main()
{
	int n=inn();rep(i,1,n) p[i]=inn(),in[p[i]]++;
	//unsigned int QwQ=1,t=1;rep(i,1,n) QwQ+=p[i]*t,t*=998244353;cout<<QwQ<<endl;
	for(int i=1,x;i<=n;i++)
	{
		for(x=i;!vis[x];x=p[x]) vis[x]=i;
		if(vis[x]^i) continue;
		for(cyc_cnt++;!onc[x];x=p[x])
			onc[x]=cyc_cnt,sz[cyc_cnt]++;
	}
//	debug(cyc_cnt)ln;
//	rep(i,1,cyc_cnt) debug(i)sp,debug(sz[i])ln;
//	rep(i,1,n) debug(i)sp,debug(onc[i])sp,debug(in[i])ln;cerr ln;
	memset(vis,0,sizeof(int)*(n+1));
	for(int i=1,x,c;i<=n;i++) if(!in[i])
	{
		for(x=i,c=0;!onc[x];vis[x]=1,x=p[x],c++)
			if(vis[x]) return !printf("0\n");
		if(vis[x]) return !printf("0\n");vis[x]=1,l[x]=c;
	}
	rep(i,1,n) if(onc[i]&&l[i]) jhs[onc[i]]=1;
	rep(i,1,cyc_cnt) if(!jhs[i]) cnt[sz[i]]++;
//	rep(i,1,cyc_cnt) debug(i)sp,debug(jhs[i])sp,debug(sz[i])ln;
//	rep(i,1,n) debug(i)sp,debug(l[i])sp,debug(cnt[i])ln;cerr ln;
	prelude(n);int ans=1;
	for(int i=1,k,t,s,v;i<=n;i++) if(cnt[i])
	{
		rep(j,it[0]=1,cnt[i]/2) it[j]=(lint)it[j-1]*i%mod;
		for(k=cnt[i],t=0,s=0;t<=cnt[i]/2;t++)
			v=(lint)C(k,2*t)*f[t]%mod*it[t]%mod,
			(((i&1)&&i>1)?v=(lint)v*mi[k-2*t]%mod:0),
			s+=v,(s>=mod?s-=mod:0);
		ans=(lint)ans*s%mod;
	}
	for(int i=1,x,c;i<=n;i++) if(onc[i]&&l[i])
	{	for(x=i,c=1;!l[p[x]];x=p[x],c++);pre[p[x]]=c;	}
//	rep(i,1,n) debug(i)sp,debug(pre[i])ln;
	rep(i,1,n) if(onc[i]&&l[i])
	{
		if(l[i]>pre[i]) return !printf("0\n");
		else if(l[i]<pre[i]) ans=ans*2,(ans>=mod?ans-=mod:0);
	}
	return !printf("%d\n",ans);
}