#include <iostream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <cassert>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <string>
#include <algorithm>
#include <utility>
#define llint long long
#define inf 1e18
#define rep(x, s, t) for(llint (x) = (s); (x) < (t); (x)++)
#define Rep(x, s, t) for(llint (x) = (s); (x) <= (t); (x)++)
#define chmin(x, y) (x) = min((x), (y))
#define chmax(x, y) (x) = max((x), (y))
#define mod 1000000007

using namespace std;
typedef pair<llint, llint> P;

struct UnionFind{
	int size;
	vector<int> parent;
	
	UnionFind(){}
	UnionFind(int size){
		this->size = size;
		parent.resize(size+1);
		init();
	}
	void init(){
		for(int i = 0; i <= size; i++) parent[i] = i;
	}
	int root(int i){
		if(parent[i] == i) return i;
		return parent[i] = root(parent[i]);
	}
	bool same(int i, int j){
		return root(i) == root(j);
	}
	void unite(int i, int j){
		int root_i = root(i), root_j = root(j);
		if(root_i == root_j) return;
		parent[root_i] = root_j;
	}
};
const int FACT_MAX = 200005;
llint fact[FACT_MAX], fact_inv[FACT_MAX];

llint modpow(llint a, llint n)
{
	if(n == 0) return 1;
	if(n % 2){
		return ((a%mod) * (modpow(a, n-1)%mod)) % mod;
	}
	else{
		return modpow((a*a)%mod, n/2) % mod;
	}
}

void make_fact()
{
	llint val = 1;
	fact[0] = 1;
	for(int i = 1; i < FACT_MAX; i++){
		val *= i;
		val %= mod;
		fact[i] = val;
	}
	fact_inv[FACT_MAX-1] = modpow(fact[FACT_MAX-1], mod-2);
	for(int i = FACT_MAX-2; i >= 0; i--){
		fact_inv[i] = fact_inv[i+1] * (i+1) % mod;
	}
}

llint comb(llint n, llint k)
{
	llint ret = 1;
	ret *= fact[n];
	ret *= fact_inv[k], ret %= mod;
	ret *= fact_inv[n-k], ret %= mod;
	return ret;
}

llint n;
llint p[100005];
vector<llint> q[100005];
UnionFind uf(100005);
map<llint, vector<llint> > mp;
bool used[100005];
llint cnt[100005];

llint dfs(int v, vector<llint> &vec)
{
	used[v] = true;
	llint ret;
	if(used[p[v]]) ret = p[v];
	else ret = dfs(p[v], vec);
	
	if(ret){
		vec.push_back(v);
		if(ret == v) ret = 0;
	}
	return ret;
}

llint getlen(llint v, llint p)
{
	llint ret = 0, cnt = 0;
	for(int i = 0; i < q[v].size(); i++){
		if(q[v][i] == p) continue;
		llint res = getlen(q[v][i], p);
		if(res == -1) return -1;
		ret = res + 1;
		cnt++;
	}
	if(cnt >= 2) return -1;
	return ret;
}

int main(void)
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	make_fact();
	
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> p[i];
	for(int i = 1; i <= n; i++){
		q[p[i]].push_back(i);
		uf.unite(i, p[i]);
	}
	for(int i = 1; i <= n; i++){
		if(q[i].size() >= 3){
			cout << 0 << endl;
			return 0;
		}
	}
	
	for(int i = 1; i <= n; i++){
		if(mp.count(uf.root(i))) continue;
		dfs(i, mp[uf.root(i)]);
	}
	
	llint ans = 1;
	for(auto it = mp.begin(); it != mp.end(); it++){
		vector<llint> &vec = it->second;
		//for(int i = 0; i < vec.size(); i++) cout << vec[i] << " "; cout << endl;
		vector<llint> vec2;
		llint m = vec.size();
		for(int i = 0; i < vec.size(); i++){
			llint res = getlen(vec[i], vec[(i+1)%m]);
			if(res == -1){
				cout << 0 << endl;
				return 0;
			}
			vec2.push_back(res);
		}
		//for(int i = 0; i < vec2.size(); i++) cout << vec2[i] << " "; cout << endl;
		
		bool flag = false;
		for(int i = 0; i < vec.size(); i++){
			if(vec2[i] == 0) continue;
			flag = true;
			llint d;
			for(int j = 1; j <= m; j++){
				if(vec2[(i+j)%m] > 0){
					d = j;
					break;
				}
			}
			if(vec2[i] > d){
				cout << 0 << endl;
				return 0;
			}
			if(vec2[i] < d) ans *= 2, ans %= mod;
		}
		if(!flag) cnt[m]++;
	}
	
	for(int i = 1; i <= n; i++){
		if(cnt[i] == 0) continue;
		llint mul = 1, sum = 0;
		for(int j = 0; 2*j <= cnt[i]; j++){
			llint tmp = mul * modpow(i, j) % mod;
			tmp *= fact_inv[j], tmp %= mod;
			//cout << i << " " << j << " " << tmp << endl;
			if(i % 2 && i > 1) tmp *= modpow(2, cnt[i]-2*j), tmp %= mod;
			sum += tmp, sum %= mod;
			mul *= comb(cnt[i]-2*j, 2), mul %= mod;
		}
		//cout << i << " " << sum << endl;
		ans *= sum, ans %= mod;
	}
	cout << ans << endl;
	
	return 0;
}

Measured memory_bytes: 21120