#include <bits/stdc++.h>
#define ll long long
#define file(s) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
using namespace std;
const int N = 200010, MOD = 1e9 + 7;

#define inc(i, x, y) for (int i = x; i <= y; ++i)
#define dec(i, x, y) for (int i = x; i >= y; --i)
#define gc getchar

bool chkmin(int &x, int y) { return x > y ? x = y, 1 : 0; }
bool chkmax(int &x, int y) { return x < y ? x = y, 1 : 0; }

int rd() {
	int ret = 0; bool fl = 0; char ch = 0;
	while (!isdigit(ch)) fl |= (ch == '-'), ch = gc();
	while (isdigit(ch)) ret = (ret << 1) + (ret << 3) + (ch ^ 48), ch = gc();
	return fl ? -ret : ret;
}

ll f[N], finv[N], meth[N];

ll qpow(ll a, ll b) {
    ll re = 1;
    while (b) {
        if (b & 1)
            re = (re * a) % MOD;
        a = a * a % MOD;
        b >>= 1;
    }
    return re;
}

void init() {
    ll i, len = 200000;
    f[0] = f[1] = finv[0] = finv[1] = 1;
    for (i = 2; i <= len; i++) f[i] = f[i - 1] * i % MOD;
    finv[len] = qpow(f[len], MOD - 2);
    for (i = len; i > 2; i--) finv[i - 1] = finv[i] * i % MOD;
}

int n, a[N], vis[N], cir[N], cntcir = 0, in[N], bst[N], siz[N];
ll ans = 1;
vector<int> s;
vector<int> nd[N];
bool cmp(int l, int r) { return siz[l] < siz[r]; }

ll C(ll x, ll y) { return f[x] * finv[y] % MOD * finv[x - y] % MOD; }

int main() {
    n = rd();
    int i, j;
    ll tmp, c, cc;
    init();
    meth[0] = 1;
    for (i = 1; i <= n; i++) meth[i] = C(i * 2, i) * f[i] % MOD * qpow(qpow(2, MOD - 2), i) % MOD;

    for (i = 1; i <= n; i++) a[i] = rd(), in[a[i]]++;
    for (i = 1; i <= n; i++) {  
        j = i;
        while (!vis[j]) vis[j] = i, j = a[j];
        if (vis[j] ^ i)
            continue;
        cntcir++;
        while (!cir[j]) {
            cir[j] = cntcir, nd[cntcir].push_back(j), siz[cntcir]++, j = a[j];
        }
    }
    memset(vis, 0, sizeof(vis));
    for (i = 1; i <= n; i++) {  
        if (in[i])
            continue;
        j = i;
        while (!cir[j] && !vis[j]) j = a[j];
        if (vis[j]) {
            puts("0");
            return 0;
        }
        bst[cir[j]] = 1;
        tmp = cir[j];
        j = i;
        c = 0;
        while (!cir[j]) cir[j] = tmp, c++, vis[j] = 1, j = a[j];
        vis[j] = c;
    }
    for (i = 1; i <= cntcir; i++) {  
        if (!bst[i]) {
            s.push_back(i);
            continue;
        }
        for (j = 0; j < nd[i].size(); j++)
            if (vis[nd[i][j]])
                break;
        tmp = j;
        do {
            c = j;
            cc = 1;
            j--;
            (j += (int)nd[i].size());
            j %= (int)nd[i].size();
            for (; !vis[nd[i][j]]; j--, j = ((j < 0) ? j + nd[i].size() : j)) cc++;
            if (vis[nd[i][c]] > cc) {
                puts("0");
                return 0;
            }
            if (vis[nd[i][c]] < cc)
                (ans *= 2) %= MOD;
        } while (tmp != j);
    }
    sort(s.begin(), s.end(), cmp);
    for (i = 0; i < s.size(); i += c) {
        j = i;
        tmp = 0;
        while (siz[s[j]] == siz[s[i]] && j < s.size()) j++;
        c = j - i;
        for (j = 0; j <= c / 2; j++) {
            (tmp += C(c, 2 * j) * meth[j] % MOD * qpow(siz[s[i]], j) % MOD *
                    qpow(2, (c - 2 * j) * (siz[s[i]] != 1) * (siz[s[i]] & 1)) % MOD) %= MOD;
        }
        ans = ans * tmp % MOD;
    }
    printf("%lld\n", ans);
}

Measured memory_bytes: 18044