#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

// 入力
int N;
int A[1 << 18];

// 数え上げ
long long mod = 1000000007;
long long fact[1 << 18];
long long factinv[1 << 18];

// グラフ・連結成分の Vector
vector<int> X[1 << 18];
vector<int> Y[1 << 18];
vector<int> G[1 << 18];

// その他
int passed[1 << 18];
int col[1 << 18], cnts;
int num[1 << 18];
int dep[1 << 18];
int val[1 << 18];
bool iscycle[1 << 18];

long long modpow(long long a, long long b, long long m) {
	long long p = 1, q = a;
	for (int i = 0; i < 32; i++) {
		if ((b / (1LL << i)) % 2LL == 1) { p *= q; p %= m; }
		q *= q; q %= m;
	}
	return p;
}

long long Div(long long a, long long b, long long m) {
	return (a * modpow(b, m - 2, m)) % m;
}

long long ncr(long long n, long long r) {
	if (r < 0 || n < r) return 0;
	return (fact[n] * factinv[r] % mod) * factinv[n - r] % mod;
}

void init() {
	fact[0] = 1;
	for (int i = 1; i <= 200000; i++) fact[i] = (1LL * i * fact[i - 1]) % mod;
	for (int i = 0; i <= 200000; i++) factinv[i] = Div(1, fact[i], mod);
}

void dfs(int pos) {
	if (col[pos] >= 1) return;
	col[pos] = cnts;
	G[cnts].push_back(pos);
	for (int i : Y[pos]) dfs(i);
}

long long solve_noncycle(int idx) {
	for (int i : G[idx]) {
		int ret = 0;
		for (int j : X[i]) { if (iscycle[j] == false) ret += 1; }
		if (ret >= 2) return 0;
	}

	int root = 0;
	for (int i : G[idx]) val[i] = (1 << 30);
	for (int i : G[idx]) {
		if (X[i].size() >= 1) continue;
		int cx = i, sum = 0;
		while (true) { cx = A[cx]; sum += 1; if (iscycle[cx] == true) break; }
		dep[cx] = sum; val[A[cx]] = min(val[A[cx]], 1);
		root = cx;
	}
	
	int cx = root;
	for (int i = 0; i < 3 * G[idx].size(); i++) {
		val[A[cx]] = min(val[A[cx]], val[cx] + 1);
		cx = A[cx];
	}

	long long ret = 1;
	for (int i : G[idx]) {
		if (dep[i] == 0) continue;
		if (dep[i] > val[i]) return 0;
		if (dep[i] < val[i]) { ret *= 2LL; ret %= mod; }
	}
	return ret;
}

long long solve_cycle(int sz, int pos) {
	long long ret = 0, arr = 1;
	for (int i = 0; i <= pos / 2; i++) {
		int t = 1; if (sz >= 3 && sz % 2 == 1) t = 2;
		long long v1 = modpow(t, pos - 2 * i, mod);
		long long v2 = modpow(sz, i, mod);
		long long v3 = arr * factinv[i] % mod;
		ret += (v1 * v2 % mod) * v3 % mod;
		ret %= mod;
		arr *= ncr(pos - 2 * i, 2);
		arr %= mod;
	}
	return ret;
}

int main() {
	// ステップ 1. 入力
	init();
	cin >> N;
	for (int i = 1; i <= N; i++) cin >> A[i];
	for (int i = 1; i <= N; i++) X[A[i]].push_back(i);
	for (int i = 1; i <= N; i++) Y[A[i]].push_back(i);
	for (int i = 1; i <= N; i++) Y[i].push_back(A[i]);

	// ステップ 2. 連結成分に分ける
	for (int i = 1; i <= N; i++) {
		if (col[i] >= 1) continue;
		cnts++;
		dfs(i);
	}

	// ステップ 3. サイクルを見つける
	for (int i = 1; i <= N; i++) {
		if (passed[i] >= 1 || X[i].size() >= 1) continue;
		int cx = i;
		while (passed[cx] <= 2) { passed[cx] += 1; cx = A[cx]; }
	}
	for (int i = 1; i <= N; i++) {
		if (passed[i] == 0 || passed[i] >= 2) iscycle[i] = true;
	}

	// ステップ 4. 数え上げ
	long long Answer = 1;
	for (int i = 1; i <= cnts; i++) {
		bool flag = false;
		for (int j : G[i]) {
			if (iscycle[j] == false) flag = true;
		}
		if (flag == false) num[G[i].size()] += 1;
		else Answer *= solve_noncycle(i);
		Answer %= mod;
	}
	for (int i = 1; i <= N; i++) {
		Answer *= solve_cycle(i, num[i]);
		Answer %= mod;
	}

	// ステップ 5. 出力
	cout << Answer << endl;
	return 0;
}

Measured memory_bytes: 37984