#include <algorithm>  
#include <iostream>  
#include <sstream>  
#include <string>  
#include <cstring>
#include <vector>  
#include <queue>  
#include <set>  
#include <map>  
#include <cstdio>  
#include <cstdlib>  
#include <cctype>  
#include <cmath>  
#include <list>  
#include <cassert>
#include <ctime>
#include <climits>
using namespace std;  

#define PB push_back  
#define MP make_pair  
#define SZ(v) ((int)(v).size())  
#define FOR(i,a,b) for(int i=(a);i<(b);++i)  
#define REP(i,n) FOR(i,0,n)  
#define FORE(i,a,b) for(int i=(a);i<=(b);++i)  
#define REPE(i,n) FORE(i,0,n)  
#define FORSZ(i,a,v) FOR(i,a,SZ(v))  
#define REPSZ(i,v) REP(i,SZ(v))  
typedef long long ll;
typedef unsigned long long ull;
ll gcd(ll a,ll b) { return b==0?a:gcd(b,a%b); }

const int MAXN=100000;
const int MOD=1000000007;

int n;
int a[MAXN];

typedef struct BF {
	int bfcnt;
	int p[MAXN];
	int r[MAXN];
	void go(int at) {
		if(at>=n) {
			//REP(i,n) printf("%d ",p[i]+1); puts("");
			++bfcnt;
			return;
		}
		//printf("go(%d):",at+1); REP(i,n) printf(" %d",p[i]+1); puts("");
		int want=a[at];
		//printf("\twant=%d r[want]=%d\n",want+1,r[want]+1);
		if(r[want]!=-1) {
			if(r[want]==at) {
				assert(p[at]==want); go(at+1);
			} else if(r[r[want]]==-1) {
				p[at]=r[want]; r[r[want]]=at;
				go(at+1);
				p[at]=-1; r[r[want]]=-1;
			} else if(r[r[want]]==at) {
				assert(p[p[at]]==want); go(at+1);
			}
		} else if(p[at]!=-1) {
			if(p[p[at]]==-1) {
				p[p[at]]=want; r[want]=p[at];
				go(at+1);
				p[p[at]]=-1; r[want]=-1;
			} else {
				assert(p[p[at]]!=want);
			}
		} else {
			p[at]=want; r[want]=at;
			go(at+1);
			p[at]=-1; r[want]=-1;
			REP(i,n) if(i!=at&&i!=want&&r[i]==-1&&p[i]==-1) {
				p[at]=i; r[i]=at; p[i]=want; r[want]=i;
				go(at+1);
				p[at]=-1; r[i]=-1; p[i]=-1; r[want]=-1;
			}
		}
	}
	int solve() {
		bfcnt=0;
		memset(p,-1,sizeof(p));
		memset(r,-1,sizeof(r));
		go(0);
		return bfcnt;
	}
} BF;
BF bf;


int chead[MAXN],cnxt[MAXN];
bool done[MAXN];
int cyc[MAXN],ncyc;
int len[MAXN];
int cnt[MAXN+1];

int fac[MAXN+1];
int ifac[MAXN+1];
int inv[MAXN+1];
int p2[MAXN+1];
int ip2[MAXN+1];
int mypow(int x,int n) { int ret=1; while(true) { if(n&1) ret=(ll)ret*x%MOD; if((n>>=1)==0) return ret; x=(ll)x*x%MOD; } }

int solve() {
	REP(i,n) chead[i]=-1;
	REP(i,n) cnxt[i]=chead[a[i]],chead[a[i]]=i;
	//REP(i,n) { printf("%d:",i); for(int x=chead[i];x!=-1;x=cnxt[x]) printf(" %d",x); puts(""); }

	int ret=1;
	REP(i,n) done[i]=false;
	memset(cnt,0,sizeof(cnt));
	REP(i,n) if(!done[i]) {
		int at=i; while(!done[at]) { done[at]=true; at=a[at]; }
		int root=at; ncyc=0;
		do { cyc[ncyc++]=at; at=a[at]; } while(at!=root);
		REP(j,ncyc) {
			at=chead[cyc[j]]; if(at==cyc[(j+ncyc-1)%ncyc]) at=cnxt[at]; len[j]=0; 
			while(at!=-1) {
				int to=cnxt[at]; if(at==chead[cyc[j]]&&to==cyc[(j+ncyc-1)%ncyc]) to=cnxt[to];
				if(to!=-1) return 0;
				++len[j]; done[at]=true; at=chead[at];
			}
		}
		//REP(j,ncyc) printf("%d(%d) ",cyc[j],len[j]); puts("");
		bool any=false; REP(j,ncyc) if(len[j]) any=true;
		if(!any) { ++cnt[ncyc]; continue; }
		int cur=1;
		REP(j,ncyc) if(len[j]>0) {
			if(len[j]>ncyc) return 0;
			REP(k,len[j]-1) if(len[(j-k-1+ncyc)%ncyc]!=0) return 0;
			if(len[j]<ncyc&&len[(j-len[j]+ncyc)%ncyc]==0) cur=2*cur%MOD;
		}
		ret=(ll)ret*cur%MOD;
	}
	inv[1]=1; FORE(i,2,n) inv[i]=(ll)(MOD-(MOD/i))*inv[MOD%i]%MOD;
	fac[0]=1; FORE(i,1,n) fac[i]=(ll)fac[i-1]*i%MOD;
	ifac[0]=1; FORE(i,1,n) ifac[i]=(ll)ifac[i-1]*inv[i]%MOD;
	p2[0]=1; FORE(i,1,n) p2[i]=(ll)p2[i-1]*2%MOD;
	ip2[0]=1; FORE(i,1,n) ip2[i]=(ll)ip2[i-1]*inv[2]%MOD;
	FORE(i,1,n) if(cnt[i]>0) {
		int cur=0;
		for(int x=0;2*x<=cnt[i];++x) {
			//C[cnt[i]][2*x]*fac[2*x]/p2[x]/fac[x] * i^x
			int now=(ll)fac[cnt[i]]*ifac[cnt[i]-2*x]%MOD*ip2[x]%MOD*ifac[x]%MOD*mypow(i,x)%MOD;
			//printf("%d pairs of %d (%d rem) -> %d\n",x,i,cnt[i]-2*x,now);
			if(i!=1&&i%2==1) {
				now=(ll)now*p2[cnt[i]-2*x]%MOD;
				//printf("%d pairs of %d (%d rem) -> %d\n",x,i,cnt[i]-2*x,now);
			}
			cur=(cur+now)%MOD;
		}
		ret=(ll)ret*cur%MOD;
	}
	return ret;
}


void run() {
	scanf("%d",&n); REP(i,n) scanf("%d",&a[i]),--a[i];
	//n=MAXN; REP(i,n) a[i]=2*(i/2)+1-i%2;
	//printf("bf=%d\n",bf.solve());
	printf("%d\n",solve());
}

int main() {
	run();
	return 0;
}


Measured memory_bytes: 4608