#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<ctime>
#include<map>
#include<bitset>
#include<set>
#define LL long long
#define mp(x,y) make_pair(x,y)
#define pll pair<long long,long long>
#define pii pair<int,int>
using namespace std;
inline LL read()
{
	LL f=1,x=0;char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
int stack[20];
template<typename T>inline void write(T x)
{
	if(x<0){putchar('-');x=-x;}
    if(!x){putchar('0');return;}
    int top=0;
    while(x)stack[++top]=x%10,x/=10;
    while(top)putchar(stack[top--]+'0');
}
template<typename T>inline void pr1(T x){write(x);putchar(' ');}
template<typename T>inline void pr2(T x){write(x);putchar('\n');}
const int MAXN=300005;
const int mod=1e9+7;
int pow_mod(int a,int b)
{
	int ret=1;
	for(;b;b>>=1,a=1LL*a*a%mod)if(b&1)ret=1LL*ret*a%mod;
	return ret;
}
int n,a[MAXN],siz[MAXN],rt[MAXN],P[MAXN],Q[MAXN];
int findrt(int x){return rt[x]==x?rt[x]:rt[x]=findrt(rt[x]);}
vector<int> nxt[MAXN];
int sum[MAXN],ri[MAXN],is[MAXN],tot;
bool findring(int x,int ed)
{
	if(x==ed){is[x]=1;ri[++tot]=x;return true;}
	for(int k=0;k<nxt[x].size();k++)
	{
		int y=nxt[x][k];
		if(findring(y,ed)){is[x]=1;ri[++tot]=x;return true;}
	}return false;
}int total=0;
void dfs(int x)
{
	int o=0;
	for(int k=0;k<nxt[x].size();k++)
	{
		int y=nxt[x][k];
		if(!is[y])dfs(y),++o,++total;
	}if(o>1){puts("0");exit(0);}
}
int num[MAXN];
int pre[MAXN],inv[MAXN];
int C(int n,int m){return 1LL*pre[n]*inv[m]%mod*inv[n-m]%mod;}
void ad(int &x,int y){x+=y;if(x>=mod)x-=mod;}
int main()
{
	pre[0]=1;for(int i=1;i<MAXN;i++)pre[i]=1LL*pre[i-1]*i%mod;
	inv[MAXN-1]=pow_mod(pre[MAXN-1],mod-2);
	for(int i=MAXN-2;i>=0;i--)inv[i]=1LL*inv[i+1]*(i+1)%mod;
	n=read();
	for(int i=1;i<=n;i++)a[i]=read();
	for(int i=1;i<=n;i++)rt[i]=i;
	for(int i=1;i<=n;i++)
	{
		int p=findrt(i),q=findrt(a[i]);
		if(p!=q)rt[p]=q,P[q]|=P[p],Q[q]|=Q[p],nxt[a[i]].push_back(i);
		else P[p]=i,Q[p]=a[i];
	}int ans=1;
	for(int i=1;i<=n;i++)if(rt[i]==i)
	{
		tot=0;findring(P[i],Q[i]);
		for(int j=1;j<=tot;j++)total=0,dfs(ri[j]),num[j]=total;
		reverse(num+1,num+1+tot);
		int fi=-1;for(int j=1;j<=tot;j++)if(num[j]){fi=j;break;}
		if(fi==-1){++sum[tot];continue;}
		if(tot==1)
		{
			if(num[fi]>1)return puts("0"),0;
			continue;
		}
		for(int j=tot+1;j<=3*tot;j++)num[j]=num[j-tot];
		int cal=0,s1=1,s2=0;
		
		for(int j=fi;j<fi+num[fi];j++)if(j!=fi&&num[j])return puts("0"),0;
		for(int j=fi+num[fi],nxt=j+1;j<fi+tot;j=nxt,nxt=j+1)if(num[j])
		{
			if(j+num[j]-tot>fi){s1=0;break;}
			for(;nxt<j+num[j];nxt++)if(num[nxt]){s1=0;break;}
			if(num[j+num[j]])continue;
			s1=1LL*s1*2%mod;
		}ad(cal,s1);
		if(!num[fi+num[fi]])
		{
			s2=1;
			for(int j=fi+num[fi],nxt=j+1;j<fi+tot;j=nxt,nxt=j+1)if(num[j])
			{
				if(j+num[j]-tot>fi+1){s2=0;break;}
				for(;nxt<j+num[j];nxt++)if(num[nxt]){s2=0;break;}
				if(num[j+num[j]])continue;
				s2=1LL*s2*2%mod;
			}
		}ad(cal,s2);
		ans=1LL*ans*cal%mod;
	}
	for(int i=1;i<=n;i++)if(sum[i])
	{
		int cal=0;
		for(int j=0;2*j<=sum[i];j++)
		{
			int t=1LL*pre[sum[i]]*inv[sum[i]-2*j]%mod*inv[j]%mod*pow_mod(inv[2],j)%mod*pow_mod(i,j)%mod;
			if(j==0)t=1;
			if(i>1&&i&1)t=1LL*t*pow_mod(2,sum[i]-2*j)%mod;
			ad(cal,t);
		}
		ans=1LL*ans*cal%mod;
	}pr2(ans);
	return 0;
}


Measured memory_bytes: 28544