#include<bits/stdc++.h>
clock_t t=clock();
namespace my_std{
	using namespace std;
	#define pii pair<int,int>
	#define fir first
	#define sec second
	#define MP make_pair
	#define rep(i,x,y) for (int i=(x);i<=(y);i++)
	#define drep(i,x,y) for (int i=(x);i>=(y);i--)
	#define go(x) for (int i=head[x];i;i=edge[i].nxt)
	#define templ template<typename T>
	#define sz 101100
	#define mod 1000000007ll
	typedef long long ll;
	typedef double db;
	templ inline bool chkmax(T &x,T y){return x<y?x=y,1:0;}
	templ inline bool chkmin(T &x,T y){return x>y?x=y,1:0;}
	templ inline void read(T& t)
	{
		t=0;char f=0,ch=getchar();double d=0.1;
		while(ch>'9'||ch<'0') f|=(ch=='-'),ch=getchar();
		while(ch<='9'&&ch>='0') t=t*10+ch-48,ch=getchar();
		if(ch=='.'){ch=getchar();while(ch<='9'&&ch>='0') t+=d*(ch^48),d*=0.1,ch=getchar();}
		t=(f?-t:t);
	}
	char __sr[1<<21],__z[20];int __C=-1,__zz=0;
	inline void Ot(){fwrite(__sr,1,__C+1,stdout),__C=-1;}
	inline void print(register int x)
	{
		if(__C>1<<20)Ot();if(x<0)__sr[++__C]='-',x=-x;
		while(__z[++__zz]=x%10+48,x/=10);
		while(__sr[++__C]=__z[__zz],--__zz);__sr[++__C]='\n';
	}
	void file()
	{
		#ifdef NTFOrz
		freopen("a.in","r",stdin);
		#endif
	}
	inline void chktime()
	{
		#ifdef NTFOrz
		cout<<(clock()-t)/1000.0<<'\n';
		#endif
	}
	#ifdef mod
	ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x%mod) if (y&1) ret=ret*x%mod;return ret;}
	ll inv(ll x){return ksm(x,mod-2);}
	#else
	ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x) if (y&1) ret=ret*x;return ret;}
	#endif
//	inline ll mul(ll a,ll b){ll d=(ll)(a*(double)b/mod+0.5);ll ret=a*b-d*mod;if (ret<0) ret+=mod;return ret;}
}
using namespace my_std;

int n;
int V[sz];
int deg[sz];

struct hh{int t,nxt;}edge[sz<<1];
int head[sz],ecnt;
void make_edge(int f,int t)
{
	edge[++ecnt]=(hh){t,head[f]};
	head[f]=ecnt;
	edge[++ecnt]=(hh){f,head[t]};
	head[t]=ecnt;
}

int cnt[sz];
int vis[sz];
int pt[sz],cc;
#define v edge[i].t
void dfs(int x)
{
	vis[x]=1;pt[++cc]=x;
	go(x) if (!vis[v]) dfs(v);
}
int dfs2(int x,int f1,int f2)
{
	int ret=0,cc=0;
	go(x) if (v!=f1&&v!=f2) ++cc,ret=dfs2(v,x,x)+1;
	if (cc>1) return -1e9;
	return ret;
}

int dn[sz],tt[sz];
ll work(int x)
{
	int cir=1;
	vector<int>ci;ci.push_back(x);
	int xx=V[x];tt[x]=1;
	for (xx=V[x];!tt[xx];xx=V[xx]) ++cir,ci.push_back(xx),tt[xx]=1;
	if (xx!=x) return 0;
	reverse(ci.begin()+1,ci.end());
	rep(i,0,cir-1) dn[i]=dfs2(ci[i],ci[(i+1)%cir],ci[(i-1+cir)%cir]);
	rep(i,0,cir-1) if (dn[i]<0) return 0;
	if (dn[cir-1]>1) return 0;
	int lst=cir-1;while (!dn[lst]) --lst;
	if (cir-1-lst+1<dn[lst]) return 0;
	ll ret;if (cir-1-lst+1==dn[lst]) ret=1; else ret=2;
	int L=lst;
	drep(i,L-1,0) if (dn[i])
	{
		if (lst-i<dn[i]) return 0;
		if (lst-i>dn[i]) ret=ret*2%mod;
		lst=i;
	}
	return ret;
}

ll fac[sz],_fac[sz];
void init(){_fac[0]=fac[0]=1;rep(i,1,sz-1) _fac[i]=inv(fac[i]=fac[i-1]*i%mod);}
ll C(int n,int m){return n>=m&&m>=0?fac[n]*_fac[m]%mod*_fac[n-m]%mod:0;}
ll calc(int n,int len)
{
	ll ret=0,I=inv(2);
	rep(i,0,n) if (!(i&1)) 
		(ret+=C(n,i)*C(i,i/2)%mod*fac[i/2]%mod*ksm(I,i/2)%mod*ksm(len,i/2)%mod*(((len&1)&&len!=1)?ksm(2,n-i):1))%=mod;
	return ret;
}

int main()
{
	file(); 
	init();
	read(n);
	rep(i,1,n) read(V[i]),++deg[V[i]],make_edge(i,V[i]);
	ll ans=1;
	rep(i,1,n) if (!vis[i])
	{
		cc=0;dfs(i);
		int flg=1,c=0;
		rep(i,1,cc) if (deg[pt[i]]!=1) c=(deg[pt[i]]>1?pt[i]:c),flg=0;
		if (flg) { ++cnt[cc]; continue; }
		ans=ans*work(c)%mod;
	}
	rep(i,1,n) if (cnt[i]) ans=ans*calc(cnt[i],i)%mod;
	cout<<ans;
	return 0;
}

Measured memory_bytes: 11132