#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#include<ctime>
#include<cassert>
using namespace std;
#define y0 y0z
#define y1 y1z
#define yn ynz
#define j0 j0z
#define j1 j1z
#define jn jnz
#define tm tmz
#define buli(x) (__builtin_popcountll(x))
#define bur0(x) (__builtin_ctzll(x))
#define bul2(x) (63-__builtin_clzll(x))
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define fil(a,b) memset((a),(b),sizeof(a))
#define cl(a) fil(a,0)
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define rep(i,a,b) for (int i=(a),_ed=(b);i<_ed;i++)
#define per(i,a,b) for (int i=(b)-1,_ed=(a);i>=_ed;i--)
#define forg(i,gu) for (int i=gu;~i;i=e[i].next)
#define pw(x) ((ll(1))<<(x))
#define upmo(a,b) (((a)=((a)+(b))%mo)<0?(a)+=mo:(a))
#define mmo(a,b) (((a)=1ll*(a)*(b)%mo)<0?(a)+=mo:(a))
void getre(){int x=0;printf("%d\n",1/x);}
void gettle(){int res=1;while(1)res<<=1;printf("%d\n",res);}
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
template<typename T,typename S>inline bool upmin(T&a,const S&b){return a>b?a=b,1:0;}
template<typename T,typename S>inline bool upmax(T&a,const S&b){return a<b?a=b,1:0;}
template<typename N,typename PN>inline N flo(N a,PN b){return a>=0?a/b:-((-a-1)/b)-1;}
template<typename N,typename PN>inline N cei(N a,PN b){return a>0?(a-1)/b+1:-(-a/b);}
template<typename N>N gcd(N a,N b){return b?gcd(b,a%b):a;}
template<typename N>inline int sgn(N a){return a>0?1:(a<0?-1:0);}
#if ( ( _WIN32 || __WIN32__ ) && __cplusplus < 201103L)
#define lld "%I64d"
#else
#define lld "%lld"
#endif
inline void gn(long long&x){
	int sg=1;char c;while(((c=getchar())<'0'||c>'9')&&c!='-');c=='-'?(sg=-1,x=0):(x=c-'0');
	while((c=getchar())>='0'&&c<='9')x=x*10+c-'0';x*=sg;
}
inline void gn(int&x){long long t;gn(t);x=t;}
inline void gn(unsigned long long&x){long long t;gn(t);x=t;}
inline void gn(double&x){double t;scanf("%lf",&t);x=t;}
inline void gn(long double&x){double t;scanf("%lf",&t);x=t;}
inline void gs(char *s){scanf("%s",s);}
inline void gc(char &c){while((c=getchar())>126 || c<33);}
inline void pc(char c){putchar(c);}
#ifdef JCVB
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif
typedef long long ll;
typedef double db;
inline ll sqr(ll a){return a*a;}
inline db sqrf(db a){return a*a;}
const int inf=0x3f3f3f3f;
const db pi=3.14159265358979323846264338327950288L;
const db eps=1e-6;
const int mo=1e9+7;
int qp(int a,ll b){int n=1;do{if(b&1)n=1ll*n*a%mo;a=1ll*a*a%mo;}while(b>>=1);return n;}

/*int fa[111111];
int gf(int x){
	return x==fa[x]?x:fa[x]=gf(fa[x]);
}*/
int n;
int a[111111];
int deg[111111];
int vis[111111];
vi chd[111111];


bool oncyc[111111];
int cyc[111111];
int cytot;
int tai[111111];

int dfs(int u){
	vis[u]=1;
	int deg=0;
	int an=0;
	rep(i,0,siz(chd[u])){
		int v=chd[u][i];
		if(oncyc[v])continue;
		++deg;
		an=1+dfs(v);
	}
	if(deg>=2){
		printf("0\n");
		exit(0);
	}
	return an;
}

int ans=1;
int pure[111111];
void work(int u){
	while(!vis[u]){
		vis[u]=1;
		u=a[u];
	}
	cytot=1;
	cyc[0]=u;
	int x=a[u];
	while(x!=u){
		cyc[cytot++]=x;
		x=a[x];
	}
	cyc[cytot]=u;
	rep(i,0,cytot)oncyc[cyc[i]]=1;
	int hastail=0;
	rep(i,0,cytot){
		tai[i]=dfs(cyc[i]);
		if(tai[i])hastail=1;
	}
	tai[cytot]=tai[0];

	if(!hastail){
		pure[cytot]++;
		return;
	}
	if(cytot==1){
		if(tai[0]>=2){
			printf("0\n");
			exit(0);
		}
		return;
	}
	rep(i,0,cytot){
		int x=tai[i];
		int cur=(i-1+cytot)%cytot;
		rep(j,0,x){
			if(j!=x-1 && tai[cur]>0){
				printf("0\n");
				exit(0);
			}
			if(j==x-1){
				if(tai[cur]==0)mmo(ans,2);
			}
			cur=(cur-1+cytot)%cytot;
		}
	}
}

int fac[111111];
int ifac[111111];
int main()
{
#ifdef JCVB
	//freopen("1.in","r",stdin);
	//freopen("1.out","w",stdout);
	int _time_jc=clock();
#endif
	fac[0]=1;
	rep(i,1,101111)fac[i]=1ll*fac[i-1]*i%mo;
	rep(i,0,101111)ifac[i]=qp(fac[i],mo-2);
	gn(n);
/*	rep(i,1,n+1){
		fa[i]=i;
	}*/
	rep(i,1,n+1){
		gn(a[i]);
		//fa[gf(a[i])]=i;
		deg[a[i]]++;
	}
	rep(i,1,n+1)if(deg[i]>2){
		printf("0\n");
		return 0;
	}
	rep(i,1,n+1)chd[a[i]].pb(i);
	
	rep(i,1,n+1)if(!vis[i]){
		work(i);
	}
	rep(i,1,n+1)if(pure[i]){
		int xx=0;
		if((i&1) && i>2){
			for (int k=0;k*2<=pure[i];k++){
				int tmp=fac[pure[i]];
				mmo(tmp,qp(qp(2,mo-2),k));
				mmo(tmp,qp(i,k));
				mmo(tmp,ifac[pure[i]-k*2]);
				mmo(tmp,qp(2,pure[i]-k*2));
				mmo(tmp,ifac[k]);
				upmo(xx,tmp);
			}
		}else{
			for (int k=0;k*2<=pure[i];k++){
				int tmp=fac[pure[i]];
				mmo(tmp,qp(qp(2,mo-2),k));
				mmo(tmp,qp(i,k));
				mmo(tmp,ifac[pure[i]-k*2]);
				mmo(tmp,ifac[k]);
				upmo(xx,tmp);
			}
		}
		mmo(ans,xx);
	}

	printf("%d\n",ans);

#ifdef JCVB
	debug("time: %d\n",int(clock()-_time_jc));
#endif
	return 0;
}




Measured memory_bytes: 10752