#ifndef CLASS_MODINT
#define CLASS_MODINT

#include <cstdint>

template <std::uint32_t mod>
class modint {
private:
	std::uint32_t n;
public:
	modint() : n(0) {};
	modint(std::int64_t n_) : n((n_ >= 0 ? n_ : mod - (-n_) % mod) % mod) {};
	static constexpr std::uint32_t get_mod() { return mod; }
	std::uint32_t get() const { return n; }
	bool operator==(const modint& m) const { return n == m.n; }
	bool operator!=(const modint& m) const { return n != m.n; }
	modint& operator+=(const modint& m) { n += m.n; n = (n < mod ? n : n - mod); return *this; }
	modint& operator-=(const modint& m) { n += mod - m.n; n = (n < mod ? n : n - mod); return *this; }
	modint& operator*=(const modint& m) { n = std::uint64_t(n) * m.n % mod; return *this; }
	modint operator+(const modint& m) const { return modint(*this) += m; }
	modint operator-(const modint& m) const { return modint(*this) -= m; }
	modint operator*(const modint& m) const { return modint(*this) *= m; }
	modint inv() const { return (*this).pow(mod - 2); }
	modint pow(std::uint64_t b) const {
		modint ans = 1, m = modint(*this);
		while (b) {
			if (b & 1) ans *= m;
			m *= m;
			b >>= 1;
		}
		return ans;
	}
};

#endif // CLASS_MODINT

#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
using mint = modint<1000000007>;
int main() {
	int N;
	cin >> N;
	vector<mint> fact(N + 1), inv(N + 1), factinv(N + 1);
	fact[0] = 1; inv[1] = 1; factinv[0] = 1;
	for (int i = 1; i <= N; ++i) fact[i] = fact[i - 1] * i;
	for (int i = 2; i <= N; ++i) inv[i] = inv[mint::get_mod() % i] * mint(-int(mint::get_mod() / i));
	for (int i = 1; i <= N; ++i) factinv[i] = factinv[i - 1] * inv[i];
	function<mint(int, int)> comb = [&](int x, int y) {
		if (y < 0 || x < y) return mint(0);
		return fact[x] * factinv[y] * factinv[x - y];
	};
	vector<int> p(N), rp(N, -1), deg(N);
	for (int i = 0; i < N; ++i) {
		cin >> p[i], --p[i];
	}
	vector<vector<int> > cycle(N);
	vector<int> state(N, -1);
	for (int i = 0; i < N; ++i) {
		if (state[i] == -1) {
			int pos = i;
			while (state[pos] == -1) state[pos] = -(i + 2), pos = p[pos];
			while (state[pos] == -(i + 2)) state[pos] = i, cycle[i].push_back(pos), pos = p[pos];
		}
	}
	for (int i = 0; i < N; ++i) {
		if (state[i] <= -2) {
			rp[p[i]] = i;
			++deg[p[i]];
		}
	}
	if (*max_element(deg.begin(), deg.end()) >= 2) {
		cout << 0 << endl;
	}
	else {
		vector<int> cs(N + 1);
		mint ans = 1;
		for (int i = 0; i < N; ++i) {
			if (cycle[i].empty()) continue;
			int S = cycle[i].size();
			vector<int> seq(S);
			for (int j = 0; j < S; ++j) {
				int pos = cycle[i][j], cur = j;
				while (rp[pos] != -1) {
					pos = rp[pos];
					++seq[cur];
					cur = (cur + S - 1) % S;
				}
			}
			if (seq == vector<int>(S, 0)) {
				++cs[S];
			}
			else {
				for (int i = 0; i < S; ++i) {
					if (seq[i] == 1 && seq[(i + 1) % S] == 0) {
						ans *= 2;
					}
					else if (seq[i] >= 2) ans = 0;
				}
			}
		}
		for (int i = 1; i <= N; ++i) {
			mint sub = 0;
			for (int j = 0; j <= cs[i]; j += 2) {
				mint subsub = comb(cs[i], j) * fact[j] * mint(inv[2] * i).pow(j / 2) * factinv[j / 2];
				if (i >= 3 && i % 2 == 1) subsub *= mint(2).pow(cs[i] - j);
				sub += subsub;
			}
			ans *= sub;
		}
		cout << ans.get() << endl;
	}
	return 0;
}

Measured memory_bytes: 8832