#include<bits/stdc++.h>
#define FOR(i,a,b) for (register int i=(a);i<=(b);i++)
#define For(i,a,b) for (register int i=(a);i>=(b);i--)
#define mem(i,j) memset(i,j,sizeof(i))
#define GO(u) for (register int j=f[u];j!=-1;j=nxt[j])
#define fi first
#define se second
#define pii pair<int,int>
#define MP make_pair
#define pb push_back
using namespace std;
typedef long long ll;
const int N=1e6+5;
const int mod=1e9+7;
int n,a[N],du[N],cnt=0,vis[N],cir[N],tag[N],len[N],lth[N],s[N];
int fac[N],inv[N],er[N],stck[N],top=0,ans=1;
int tot=0,f[N],nxt[N];
struct E
{
	int u,v;
}e[N];
inline int read()
{
	int x=0,f=1;
	char c=getchar();
	while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
	while (c>='0'&&c<='9') {x=(x<<1)+(x<<3)+c-'0';c=getchar();}
	return f*x;
}
inline void write(int x)
{
	if (x<0) putchar('-'),x=-x;
	if (x>9) write(x/10);
	putchar(x%10+'0');
	return;
}
inline void no() {printf("0\n");exit(0);}
inline int qpow(int x,int y) {int ret=1;for (;y;y>>=1,x=1LL*x*x%mod) if (y&1) ret=1LL*ret*x%mod;return ret;}
inline int Inv(int x) {return qpow(x,mod-2);}
inline void Init()
{
	er[0]=fac[0]=1;
	FOR(i,1,n) er[i]=1LL*er[i-1]*2%mod,fac[i]=1LL*fac[i-1]*i%mod;
	inv[n]=Inv(fac[n]);
	For(i,n-1,0) inv[i]=1LL*inv[i+1]*(i+1)%mod;
	return;
}
inline int C(int x,int y)
{
	int ret=1LL*fac[x]*inv[y]%mod*inv[x-y]%mod;
	return ret;
}
inline void Work1()//找环+判无解
{
	queue <int> Q;
	while (Q.size()) Q.pop();
	int d[N];
	FOR(i,1,n) d[i]=du[i];
	FOR(i,1,n) if (!d[i]) Q.push(i);
	while (Q.size())
	{
		int tmp=Q.front();Q.pop();
		d[a[tmp]]--;
		if (!d[a[tmp]]) Q.push(a[tmp]);
		vis[tmp]=-1;
	}
	FOR(i,1,n) if (vis[i]==0)
	{
		int now=i;
		cnt++;
		while (!vis[now]) vis[now]=1,cir[now]=cnt,now=a[now];
	}
	FOR(i,1,n) if (vis[i]==-1) if (du[i]>1) no();
	FOR(i,1,n) if (vis[i]==1) if (du[i]>2) no();
//	printf("vis : ");FOR(i,1,n) printf("%d ",vis[i]);putchar('\n');
//	printf("cir : ");FOR(i,1,n) printf("%d ",cir[i]);putchar('\n');
	return;
}
inline void add(int u,int v)
{
	tot++;
	nxt[tot]=f[u];
	f[u]=tot;
	e[tot]=(E){u,v};
	return;
}
inline int dfs(int u,int fa) {GO(u) if (e[j].v!=fa&&!cir[e[j].v]) return dfs(e[j].v,u)+1;return 0;}
inline void Work2()//找基环树+找链长度+按长度统计环 
{
	FOR(i,1,n) add(a[i],i);
	FOR(i,1,n) vis[i]=0;
	FOR(i,1,n) if (cir[i]) if (!vis[i])
	{
		int now=i,jihuan=0,l=0;
		while (!vis[now])
		{
			vis[now]=1;
			GO(now) if (!cir[e[j].v]) jihuan=1;
			len[now]=dfs(now,0);
			now=a[now];
			l++;
		}
		tag[cir[i]]=jihuan;
		if (!jihuan) lth[cir[i]]=l;
	}
	FOR(i,1,cnt) s[lth[i]]++;
//	printf("tag : ");FOR(i,1,cnt) printf("%d ",tag[i]);putchar('\n');
//	printf("len : ");FOR(i,1,n) printf("%d ",len[i]);putchar('\n');
//	printf("lth : ");FOR(i,1,cnt) printf("%d ",lth[i]);putchar('\n');
	return;
}
int aa[N],bb[N];
inline void DP(int u)
{
	aa[0]=bb[0]=0;
	int val=1;
	while (!len[u]) u=a[u];
	int st=u;
	u=a[u];
	int step=1;
	while (u!=st)
	{
		while (!len[u]) step++,u=a[u];
		bb[++bb[0]]=step;
		aa[++aa[0]]=len[u];
		step=0;
		if (u!=st) u=a[u],step++;
	}
	FOR(i,1,aa[0])
	{
		if (aa[i]>bb[i]) val=0;
		else if (aa[i]<bb[i]) val=2LL*val%mod;
	}
	stck[++top]=val;
	return;
}
inline void Solve(int x)
{
	int val=0;
	if ((x&1)&&(x!=1)) FOR(i,1,s[x]/2) val=(1LL*val+1LL*C(s[x],2*i)*C(2*i,i)%mod*fac[i]%mod*qpow(x,i)%mod*er[s[x]-2*i]%mod*Inv(er[i])%mod)%mod;
	else FOR(i,1,s[x]/2) val=(1LL*val+1LL*C(s[x],2*i)*C(2*i,i)%mod*fac[i]%mod*qpow(x,i)%mod*Inv(er[i])%mod)%mod;
	if ((x&1)&&(x!=1)) val=(1LL*val+er[s[x]])%mod;
	else val=(1LL*val+1)%mod;
	stck[++top]=val;
	return;
}
int used[N];
inline void Work3()//统计每个连通块的方案数 
{
	mem(used,0);
	FOR(i,1,n) vis[i]=0;
	FOR(i,1,n) if (cir[i]&&(!lth[cir[i]])&&(!vis[cir[i]])) DP(i),vis[cir[i]]=1;//基环树
	FOR(i,1,cnt) if (lth[i]&&!used[lth[i]]) Solve(lth[i]),used[lth[i]]=1;//环
	return;
}
int main()
{
//	freopen("16.in","r",stdin);
//	freopen("dayinf.out","w",stdout);
	mem(f,-1);
	n=read();
	FOR(i,1,n) a[i]=read(),du[a[i]]++;
	Init();
	Work1();
	Work2();
	Work3();
	FOR(i,1,top) ans=1LL*ans*stck[i]%mod;
	write(ans);
	return 0;
}

Measured memory_bytes: 45312