#include "iostream"
#include "climits"
#include "list"
#include "queue"
#include "stack"
#include "set"
#include "functional"
#include "algorithm"
#include "string"
#include "map"
#include "unordered_map"
#include "unordered_set"
#include "iomanip"
#include "cmath"
#include "random"
#include "bitset"
#include "cstdio"
#include "numeric"
#include "cassert"

using namespace std;

const long long int MOD = 1000000007;
//const int MOD = 998244353;

long long int N, M, K, H, W, L, R;
//int N, M, K, H, W, L, R

long long int power(long long int x, long long int n, long long int M) {
	long long int tmp = 1;

	if (n > 0) {
		tmp = power(x, n / 2, M);
		if (n % 2 == 0) tmp = (tmp*tmp) % M;
		else tmp = (((tmp*tmp) % M)*x) % M;
	}
	return tmp;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(0);

	cin >> N;
	vector<int>v(N + 1);
	vector<int>dir(N + 1);
	vector<int>used(N + 1);
	vector<int>leg(N + 1);
	for (int i = 1; i <= N; i++) {
		cin >> v[i];
		dir[v[i]]++;
		if (dir[v[i]] > 2) {
			cout << 0 << endl;
			return 0;
		}
	}
	vector<int>loop(N + 1, -1);
	queue<int>Q;
	for (int i = 1; i <= N; i++) {
		if (!dir[i])Q.push(i);
	}
	while (!Q.empty()) {
		int cn = Q.front();
		Q.pop();
		int nx = v[cn];
		used[nx]++;
		leg[nx] = leg[cn] + 1;
		leg[cn] = 0;
		if (dir[nx] - used[nx])continue;
		Q.push(nx);
	}
	for (int i = 1; i <= N; i++) {
		loop[i] = dir[i] - used[i];
	}
	for (int i = 1; i <= N; i++) {
		if (!loop[i] && (dir[i] - used[i] || dir[i]>1)) {
			cout << 0 << endl;
			return 0;
		}
	}
	vector<int>num(N + 1);
	vector<int>dist(N + 1);
	for (int i = 1; i <= N; i++) {
		if (loop[i] != 1)continue;
		if (dir[i] < 2)continue;
		int node = i;
		int cnt = 0;
		do {
			cnt++;
			node = v[node];
			loop[node] = 2;
			if (dir[node] == 2) {
				dist[node] = cnt;
				cnt = 0;
			}
		} while (node != i);
	}
	for (int i = 1; i <= N; i++) {
		if (loop[i] != 1)continue;
		int node = i;
		int cnt = 0;
		int sz = 0;
		do {
			cnt++;
			node = v[node];
			loop[node] = 2;
			sz++;
		} while (node != i);
		num[sz]++;
	}
	vector<long long int>by(N + 1, 1);
	vector<long long int>rev_by(N + 1, 1);
	vector<long long int>two_by(N + 1, 1);
	for (int i = 1; i <= N; i++) {
		by[i] = by[i - 1] * i;
		by[i] %= MOD;
		two_by[i] = two_by[i - 1] * 2;
		two_by[i] %= MOD;
		rev_by[i] = power(by[i], MOD - 2, MOD);
	}
	long long int ans = 1;
	for (int i = 1; i <= N; i++) {
		if (!leg[i])continue;
		if (leg[i] > dist[i]) {
			cout << 0 << endl;
			return 0;
		}
		if (leg[i] < dist[i]) {
			ans <<= 1;
			ans %= MOD;
		}
	}
	for (int i = 1; i <= N; i++) {
		if (!num[i])continue;
		//	cout << i << " " << num[i] << endl;
		if (num[i] == 1) {
			if ((i & 1) && i > 2)ans *= 2;
			ans %= MOD;
			continue;
		}

		long long int cnt = 1;
		if ((i & 1) && i > 2) {
			cnt *= two_by[num[i]];
			cnt %= MOD;
		}
		long long int add = by[num[i]];
		add *= rev_by[2];
		add %= MOD;
		add *= rev_by[num[i] - 2];
		add %= MOD;
		add *= i;
		add %= MOD;
		if ((i & 1) && i > 2) {
			add *= two_by[num[i] - 2];
			add %= MOD;
		}
		//cout << i << " " << num[i] << endl;
		//cout << add << endl;
		for (int j = 2; j <= num[i]; j += 2) {
			cnt += add;
			cnt %= MOD;
			if (num[i] - j - 2 >= 0) {
				add *= by[num[i] - j];
				add %= MOD;
				add *= rev_by[num[i] - j - 2];
				add %= MOD;
				add *= rev_by[2];
				add %= MOD;
				if ((i & 1) && i > 2) {
					add *= power(4, MOD - 2, MOD);
					add %= MOD;
				}
				add *= i;
				add %= MOD;
				add *= by[j / 2];
				add %= MOD;
				add *= rev_by[j / 2 + 1];
				add %= MOD;
			}
			//cout << add << endl;
		}
		ans *= cnt;
		ans %= MOD;
		//	cout <<i<<" " << ans << endl;
	}
	cout << ans << endl;
	return 0;
}


Measured memory_bytes: 5504