#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cassert>
#include<ctime>
#define cmin(a,b) (a>(b)?a=(b),1:0)
#define cmax(a,b) (a<(b)?a=(b),1:0)
#define dmin(a,b) ((a)<(b)?(a):(b))
#define dmax(a,b) ((a)>(b)?(a):(b))
namespace io
{
	int F()
	{
		int n=0,F=1;
		char ch;
		while((ch=getchar())!='-'&&(ch<'0'||ch>'9'));
		ch=='-'?F=0:n=ch-'0';
		while((ch=getchar())>='0'&&ch<='9')n=n*10+ch-'0';
		return F?n:-n;
	}
}
const int M=1000000007;
int n;
int a[111111];
int g[111111];
int fac[111111];
int ifac[111111];
int ex2[111111];

int cnt[111111];
int vis[111111];

int cyc[111111];
int son[111111];
int len[111111];
int dep[111111];
int top[111111];
int ed[111111];

int v[211111],pv;
int sum[211111];
int ex(int b,int e)
{
	int ans=1;
	for(;e;e>>=1,b=(long long)b*b%M)
		if(e&1)ans=(long long)ans*b%M;
	return ans;
}
int solve()
{
	memset(cnt,0,sizeof(cnt));
	memset(vis,0,sizeof(vis));
	int fl=1;
	for(register int i=1;i<=n;++i)
	{
		if(vis[a[i]])fl=0;
		vis[a[i]]=1;
	}
	if(fl)
	{
		for(register int i=1;i<=n;++i)
			if(vis[i]!=2)
			{
				int c=0,now=i;
				while(vis[now]!=2)vis[now]=2,now=a[now],++c;
				cnt[c]++;
			}
		int ans=1;
		for(register int i=1;i<=n;++i)
			if(cnt[i])
			{
				int tmp=0;
				for(register int j=0;j<=cnt[i];j+=2)
					tmp=(tmp+(long long)g[j]*ifac[j]%M*ifac[cnt[i]-j]%M*ex2[i&1&&i>=3?cnt[i]-j:0]%M*ex(i,j>>1))%M;
				ans=ans*(long long)tmp%M*fac[cnt[i]]%M;
			}
		return ans;
	}
	else
	{
		memset(vis,0,sizeof(vis));
		memset(ed,0,sizeof(ed));
		memset(top,0,sizeof(top));
		memset(cyc,0,sizeof(cyc));
		memset(son,0,sizeof(son));
		memset(dep,0,sizeof(dep));
		int ans=1;
		int c=0;
		int pc=0;
		for(register int i=1;i<=n;++i)
			if(!vis[i])
			{
				int now=i;
				int st=c;
				while(!vis[now])vis[now]=++c,now=a[now];
				int len=c-vis[now]+1;
				if(vis[now]<=st)
				{
					if(!ed[now])return 0;
					top[i]=top[now];
					dep[i]=dep[now]+c-st;
					ed[now]=0;
					ed[i]=1;
					if(cyc[now])++son[cyc[now]];
				}
				else
				{
					cyc[now]=++pc;
					int d=a[now];
					while(d!=now)cyc[d]=ed[d]=pc,top[d]=d,d=a[d];
					top[i]=now;
					dep[i]=c-st-len;
					ed[i]=1;
					::len[pc]=len;
					son[pc]+=dep[i];
				}
			}
		for(register int i=1;i<=n;++i)
			if(top[i])cmax(dep[top[i]],dep[i]);
		for(register int i=1;i<=pc;++i)
			if(!son[i])++cnt[len[i]];
		for(register int i=1;i<=n;++i)
			if(cnt[i])
			{
				int tmp=0;
				for(register int j=0;j<=cnt[i];j+=2)
					tmp=(tmp+(long long)g[j]*ifac[j]%M*ifac[cnt[i]-j]%M*ex2[i&1&&i>=3?cnt[i]-j:0]%M*ex(i,j>>1))%M;
				ans=ans*(long long)tmp%M*fac[cnt[i]]%M;
			}
		memset(vis,0,sizeof(vis));
		for(register int i=1;i<=n;++i)
			if(cyc[i]&&!vis[i]&&son[cyc[i]])
			{
				int now=i;
				pv=0;
				while(!vis[now])vis[now]=1,v[++pv]=now,now=a[now];
				for(register int j=1;j<=pv;++j)v[pv+j]=v[j];
				for(register int j=1;j<=pv+pv;++j)sum[j]=sum[j-1]+dep[v[j]];
				for(register int j=pv+pv;j>pv;--j)
					if(dep[v[j]])
					{
						if(dep[v[j]]>pv||sum[j-1]-sum[j-dep[v[j]]])return 0;
						if(!dep[v[j-dep[v[j]]]])(ans<<=1)%=M;
					}
			}
		return ans;
	}
	return 0;
}
int main()
{
	g[0]=1;
	for(register int i=2;i<=100000;i+=2)
		g[i]=(long long)g[i-2]*(i-1)%M;
	fac[0]=1;
	for(register int i=1;i<=100000;++i)
		fac[i]=(long long)fac[i-1]*i%M;
	ifac[100000]=ex(fac[100000],M-2);
	for(register int i=99999;i>=0;--i)
		ifac[i]=(long long)ifac[i+1]*(i+1)%M;
	ex2[0]=1;
	for(register int i=1;i<=100000;++i)
		ex2[i]=(ex2[i-1]<<1)%M;
	int T=1;
	while(T--)
	{
		n=io::F();
		for(register int i=1;i<=n;++i)a[i]=io::F();
		printf("%d\n",solve());
	}
	return 0;
}

Measured memory_bytes: 7296