Problem:
Score : 1400 points

Problem Statement

You are given an integer sequence a of length N.
How many permutations p of the integers 1 through N satisfy the following condition?

For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.

Find the count modulo 10^9 + 7.

Constraints

1 ≤ N ≤ 10^5

a_i is an integer.

1 ≤ a_i ≤ N

Input

The input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.

Sample Input 1

3
1 2 3

Sample Output 1

4

The following four permutations satisfy the condition:

(1, 2, 3)

(1, 3, 2)

(3, 2, 1)

(2, 1, 3)

For example, (1, 3, 2) satisfies the condition because p_1 = 1, p_{p_2} = 2 and p_{p_3} = 3.

Sample Input 2

2
1 1

Sample Output 2

1

The following one permutation satisfies the condition:

(2, 1)

Sample Input 3

3
2 1 1

Sample Output 3

2

The following two permutations satisfy the condition:

(2, 3, 1)

(3, 1, 2)

Sample Input 4

3
1 1 1

Sample Output 4

0

Sample Input 5

13
2 1 4 3 6 7 5 9 10 8 8 9 11

Sample Output 5

6

Solution in C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#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);
			reverse(all(cycle));
			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){
		puts("0");
		exit(0);
	}
	if(G[v].size()==0) return 0;
	return dfs(G[v][0])+1;
}

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

	if(N==1) return 1;

	reverse(all(x));
	int non0 = -1;
	rep(i,N) if(x[i]>0) non0 = i;
	rotate(x.begin(),x.begin()+non0,x.end());

	mint res = 0;
	rep(t,2){
		map<int,mint> dp;
		dp[x[0]+t] = 1;
		rep1(i,N-1) if(x[i]){
			map<int,mint> ndp;
			for(auto it:dp){
				int r = it.fs;
				mint v = it.sc;
				rep(d,2){
					int l = i+d;
					if(r>l) continue;
					ndp[l+x[i]] += v;
				}
			}
			dp = ndp;
		}
		for(auto it:dp){
			int r = it.fs;
			mint v = it.sc;
			if(r<=N+t) res += v;
		}
	}
	return res;
}

int N;
vector<int> a;
int main(){
	precalc();
	cin>>N;
	a.resize(N);
	rep(i,N) cin>>a[i],a[i]--;
	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();

		vector<int> xs;
		int xsum = 0;
		rep(j,L){
			int v = cycle[j];
			int x = dfs(v);
			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;
	}
	cout<<ans<<endl;
}