#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
template<class S,class T> ostream& operator<<(ostream& o,const pair<S,T> &p){return o<<"("<<p.fs<<","<<p.sc<<")";}
template<class T> ostream& operator<<(ostream& o,const vector<T> &vc){o<<"sz = "<<vc.size()<<endl<<"[";for(const T& v:vc) o<<v<<",";o<<"]";return o;}

struct FunctionalGraph{
	//output
	vector<vector<int>> cycles;
	vector<vector<int>> G;	//a^-1 cycleの辺は除く


	vector<bool> vis;
	void setvis(int v){
		vis[v] = 1;
		for(int u:G[v]) setvis(u);
	}
	
	FunctionalGraph(vector<int> a){
		int N = a.size();
		vis.resize(N);
		G.resize(N);
		rep(i,N) G[a[i]].pb(i);
		rep(i,N) if(!vis[i]){
			int v = i;
			while(!vis[v]){
				vis[v] = 1;
				v = a[v];
			}
			vector<int> cycle;
			int s = v;
			do{
				cycle.pb(v);
				v = a[v];
			}while(v!=s);
			cycles.pb(cycle);

			int L = cycle.size();
			rep(j,L){
				int v = cycle[j];
				rep(k,G[a[v]].size()) if(G[a[v]][k] == v){
					G[a[v]].erase(G[a[v]].begin()+k);
					break;
				}
			}
			rep(j,L){
				int v = cycle[j];
				setvis(v);
			}
		}

	}

};

template<unsigned int mod_>
struct ModInt{
	using uint = unsigned int;
	using ll = long long;
	using ull = unsigned long long;

	constexpr static uint mod = mod_;

	uint v;
	ModInt():v(0){}
	ModInt(ll v):v(normS(v%mod+mod)){}
	explicit operator bool() const {return v!=0;}
	static uint normS(const uint &x){return (x<mod)?x:x-mod;}		// [0 , 2*mod-1] -> [0 , mod-1]
	static ModInt make(const uint &x){ModInt m; m.v=x; return m;}
	ModInt operator+(const ModInt& b) const { return make(normS(v+b.v));}
	ModInt operator-(const ModInt& b) const { return make(normS(v+mod-b.v));}
	ModInt operator-() const { return make(normS(mod-v)); }
	ModInt operator*(const ModInt& b) const { return make((ull)v*b.v%mod);}
	ModInt operator/(const ModInt& b) const { return *this*b.inv();}
	ModInt& operator+=(const ModInt& b){ return *this=*this+b;}
	ModInt& operator-=(const ModInt& b){ return *this=*this-b;}
	ModInt& operator*=(const ModInt& b){ return *this=*this*b;}
	ModInt& operator/=(const ModInt& b){ return *this=*this/b;}
	ll extgcd(ll a,ll b,ll &x,ll &y) const{
		ll u[]={a,1,0},v[]={b,0,1};
		while(*v){
			ll t=*u/ *v;
			rep(i,3) swap(u[i]-=t*v[i],v[i]);
		}
		if(u[0]<0) rep(i,3) u[i]=-u[i];
		x=u[1],y=u[2];
		return u[0];
	}
	ModInt inv() const{
		ll x,y;
		extgcd(v,mod,x,y);
		return make(normS(x+mod));
	}
	bool operator==(const ModInt& b) const { return v==b.v;}
	bool operator!=(const ModInt& b) const { return v!=b.v;}
	friend istream& operator>>(istream &o,ModInt& x){
		ll tmp;
		o>>tmp;
		x=ModInt(tmp);
		return o;
	}
	friend ostream& operator<<(ostream &o,const ModInt& x){ return o<<x.v;}
};
using mint = ModInt<1000000007>;

mint ex(mint x,int p){
	mint a = 1;
	while(p){
		if(p%2) a*=x;
		x*=x;
		p/=2;
	}
	return a;
}
const int MX = 200010;
mint f[MX],g[MX],inv[MX];
void precalc(){
	f[0]=1;
	rep1(i,MX-1) f[i] = f[i-1] * i;

	const int mod = 1e9+7;
	inv[1]=1;
	for(int i=2;i<=MX-1;i++) inv[i] = -inv[mod%i] * (mod/i);

	g[0]=1;
	rep1(i,MX-1) g[i] = g[i-1] *inv[i];
}


vector<vector<int>> G;
int dfs(int v){
	if(G[v].size()>1){
		return -1e9;
	}
	if(G[v].size()==0) return 0;
	return dfs(G[v][0])+1;
}

mint solve(vector<int> x){
//	show(x);
	int N = x.size();
	int sum = 0;
	rep(i,N) sum += x[i];
	if(sum>N) return 0;

	reverse(all(x));

	vector<int> c(N);
	rep(i,N){
		rep(j,x[i]) c[(i+j)%N]++;
	}
	rep(i,N) if(c[i]>1) return 0;
	mint res = 1;
	rep(i,N) if(x[i]>0){
		if(c[(i+x[i])%N]==0) res *= 2;
	}
	return res;
}

void main2(vector<int> a){
	int N = a.size();
// 	int brute = 0;
// 	{
// 		vector<int> p(N);
// 		rep(i,N) p[i] = i;
// 		do{
// 			bool ok = 1;
// 			rep(i,N){
// 				if(p[i]!=a[i] && p[p[i]]!=a[i]){
// 					ok = 0;
// 					break;
// 				}
// 			}
// 			if(ok) brute++;
// 		}while(next_permutation(all(p)));
// //		show(brute);
// 	}

	FunctionalGraph FG(a);

	G = FG.G;
	auto& cycles = FG.cycles;

	int K = cycles.size();
	mint ans = 1;
	map<int,int> cnum;

	rep(k,K){
		auto& cycle = cycles[k];
		int L = cycle.size();
//		show(cycle);

		vector<int> xs;
		int xsum = 0;
		rep(j,L){
			int v = cycle[j];
			int x = dfs(v);
			if(x<0){
				ans = 0;
				goto done;
			}
			xs.pb(x);
			xsum += x;
		}
		if(xsum==0){	//cycle
			cnum[L]++;
		}else{
			ans *= solve(xs);
		}
	}
	for(auto it:cnum){
		int L = it.fs, n = it.sc;
		mint sum = 0;
		for(int p=0;p*2<=n;p++){
			mint tmp = (f[n]/f[n-p*2]/f[p]/ex(2,p)) * ex(L,p) * ex((L==1||L%2==0)?1:2,n-p*2);
			sum += tmp;
		}
		ans *= sum;
	}
	done:
	// if(brute != ans.v){
	// 	show(a);
	// 	show(brute);
	// 	show(ans);
	// 	assert(0);
	// }
	cout<<ans<<endl;
}

int main(){
	precalc();
	// rep1(N,5){
	// 	int p = 1;
	// 	rep(i,N) p*=N;
	// 	rep(i,p){
	// 		vector<int> a(N);
	// 		int tmp = i;
	// 		rep(j,N) a[j] = tmp%N,tmp/=N;
	// 		main2(a);
	// 	}
	// }
	int N;
	vector<int> a;
	cin>>N;
	a.resize(N);
	rep(i,N) cin>>a[i],a[i]--;
	main2(a);
}


Measured memory_bytes: 16680