#include <cstdio>
#include <cstring>
using namespace std;
#define MOD 1000000007
int a[100008], d[100008], dep[100008], pre[100008], id[100008], vis[100008], q[100008], en;
int onc[100008], siz[100008], len[100008], rs[100008], ring[100008];
int fac[100008], ifac[100008], ofac[100008], pow2[100008], ans;
long long fastpow(register int a, register int n){
	register long long t, p;
	for (t = 1, p = a; n; n >>= 1, p = p * p % MOD) if (n & 1) t = p * t % MOD;
	return t;
}
int fastin(){
	register int i; register char ch;
	while (ch = getchar(), ch < '0' || ch > '9');
	i = ch - '0';
	while (ch = getchar(), ch >= '0' && ch <= '9') i = i * 10 + ch - '0';
	return i;
}
void Calc(){
	memset(d, 0, sizeof(d));
	memset(pre, 0, sizeof(pre));
	memset(id, 0, sizeof(id));
	memset(vis, 0, sizeof(vis));
	memset(siz, 0, sizeof(siz));
	memset(len, 0, sizeof(len));
	memset(onc, 0, sizeof(onc));
	register int n, i, j, u, r, to, qt, cnt, nowid;
	n = fastin();
	for (i = 1; i <= n; ++i) ++d[a[i] = fastin()];
	for (i = 1; i <= n; ++i) if (d[i] >= 3) {
		ans = 0;
		return ;
	}
	for (i = 1; i <= n; ++i) if (d[i] == 0) {
		q[dep[i] = qt = 0] = vis[i] = u = i;
		while (vis[to = a[u]] == 0) {
			q[dep[to] = ++qt] = to;
			vis[to] = i;
			u = to;
		}
		if (vis[to] == i) {
			cnt = dep[u] - dep[to];
			len[i] = cnt + 1;
			pre[to] = u;
			while (u != to) {
				onc[u] = 1;
				id[u] = i;
				pre[u] = q[qt - 1];
				u = q[--qt];
			}
			onc[u] = 1;
			id[u] = i;
			rs[i] = u;
			siz[u] = qt;
			while (qt) id[q[--qt]] = i;
		}
		else if (onc[to] == 0) {
	//fprintf(stderr, "GG\n");
			ans = 0;
			return ;
		}
		else {
			id[u] = id[to];
			siz[to] = qt + 1;
			while (qt) id[q[--qt]] = id[to];
		}
	}
	for (i = 1; i <= n; ++i) if (vis[i] == 0) {
		for (u = i, cnt = 1; a[u] != i; ++cnt, u = a[u]) vis[u] = 1;
		vis[u] = i;
		++ring[cnt];
	}
	for (i = 1; i <= n; ++i) if (ring[i]) {
		cnt = 0;
		r = ring[i];
		if ((i & 1) && i > 1) {
			cnt = pow2[r];
			u = 1;
			for (j = 2; j <= r; j += 2) {
				u = 1ll * u * i % MOD;
				cnt = (cnt + 1ll * fac[r] * ifac[j] % MOD * ifac[r - j] % MOD * pow2[r - j] % MOD * u % MOD * ofac[j - 1]) % MOD;
			}
		}
		else {
			cnt = u = 1;
			for (j = 2; j <= r; j += 2) {
				u = 1ll * u * i % MOD;
				cnt = (cnt + 1ll * fac[r] * ifac[j] % MOD * ifac[r - j] % MOD * u % MOD * ofac[j - 1]) % MOD;
			}
		}
		ans = 1ll * ans * cnt % MOD;
		//fprintf(stderr, "%d %d %d %d\n", i, r, ans, cnt);
		ring[i] = 0;
	}
	for (i = 1; i <= n; ++i) if (id[i] == i) {
		r = 1;
		u = rs[i];
		to = pre[u];
		cnt = 1;
		while (to != rs[i]) {
			//fprintf(stderr, "%d %d %d %d %d\n", i, rs[i], u, to, siz[to]);
			if (siz[to]) {
				if (cnt < siz[u]) {
					ans = 0;
					return ;
				}
				if (cnt > siz[u]) ans = (ans << 1) % MOD;
				cnt = 0;
				u = to;
			}
			++cnt;
			to = pre[to];
		}
		//fprintf(stderr, "%d %d %d\n", cnt, u, siz[u]);
		if (cnt < siz[u]) {
			ans = 0;
			return ;
		}
		if (cnt > siz[u]) ans = (ans << 1) % MOD;
	}
	return ;
}
int main(){
	register int T, i;
	fac[0] = fac[1] = ifac[0] = pow2[0] = ofac[1] = 1;
	for (i = 2; i <= 100000; ++i) fac[i] = 1ll * fac[i - 1] * i % MOD;
	ifac[100000] = fastpow(fac[100000], MOD - 2);
	for (i = 99999; i; --i) ifac[i] = 1ll * ifac[i + 1] * (i + 1) % MOD;
	for (i = 1; i <= 100000; ++i) pow2[i] = (pow2[i - 1] << 1) % MOD;
	for (i = 3; i <= 99999; i += 2) ofac[i] = 1ll * ofac[i - 2] * i % MOD;
	T = 1;
	while (T--) {
		ans = 1;
		Calc();
		printf("%d\n", ans);
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}

Measured memory_bytes: 6016