#include <bits/stdc++.h>
#define SZ(x) ((int) (x).size())
using namespace std;

typedef long long i64;

const int MOD = 1e9 + 7;

pair<int, int> getExistent(const initializer_list<int>& a) {
    int c = -1, b = -1;
    for (int p: a) {
        if (p != -1) {
            if (c == -1) {
                c = p;
            } else {
                b = p;
            }
        }
    }
    return make_pair(c, b);
}

int Pow(int x, int y) {
    int ret = 1;
    for (; y > 0; y /= 2) {
        if (y & 1) {
            ret = (int64_t) ret * x % MOD;
        }
        x = (int64_t) x * x % MOD;
    }
    return ret;
}

vector<int> fact, invFact;
vector<int> matches;

void precalc(int n) {
    fact = vector<int>(n + 1);
    fact[0] = 1;
    for (int i = 1; i <= n; ++i) {
        fact[i] = (int64_t) fact[i - 1] * i % MOD;
    }
    invFact = vector<int>(n + 1);
    invFact[n] = Pow(fact[n], MOD - 2);
    for (int i = n - 1; i >= 0; --i) {
        invFact[i] = (int64_t) invFact[i + 1] * (i + 1) % MOD;
    }
    matches = vector<int>(n + 1);
    matches[0] = 1;
    for (int i = 1; i <= n; ++i) {
        matches[i] = (int64_t) (2 * i - 1) * matches[i - 1] % MOD;
    }
}

int comb(int n, int k) {
    return (int64_t) fact[n] * invFact[n - k] % MOD * invFact[k] % MOD;
}

int solve(int n, int k) {
    const int mul = k == 1 ? 1: 1 + (k & 1);
    int ans = 0;
    for (int take = 0; 2 * take <= n; ++take) {
        int add = (int64_t) comb(n, 2 * take) * matches[take] % MOD * 
            Pow(k, take) % MOD * Pow(mul, n - 2 * take) % MOD;
        ans = (ans + add) % MOD;
    }
    return ans;
}

int main() {
    #ifdef LOCAL_RUN
    freopen("task.in", "r", stdin);
    freopen("task.out", "w", stdout);
    //freopen("task.err", "w", stderr);
    #endif // ONLINE_JUDGE
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;
    
    precalc(n);

    vector<int> A(n);
    vector<int> inv1(n, -1), inv2(n, -1);
    for (int i = 0; i < n; ++i) {
        cin >> A[i];
        A[i]--;
        if (inv1[A[i]] == -1) {
            inv1[A[i]] = i;
        } else if (inv2[A[i]] == -1) {
            inv2[A[i]] = i;
        } else {
            cout << "0\n";
            return 0;
        }
    }
    vector<bool> used(n, false);
    int ans = 1;
    for (int i = 0; i < n; ++i) {
        if (!used[i] && inv2[i] != -1) {
            int add = 1;
            int j = inv1[i], k = inv2[i];
            used[j] = used[k] = true;
            if (k == i) {
                swap(j, k);
            }
            if (j == i) {
                if (inv1[k] == -1) {
                    continue;
                } else {
                    cout << "0\n";
                    return 0;
                }
            }
            while (true) {
                if (k != -1) {
                    int deg = (inv1[j] != -1) + (inv2[j] != -1) +
                        (inv1[k] != -1) + (inv2[k] != -1);
                    if (deg > 2 || deg == 0) {
                        cout << "0\n";
                        return 0;
                    }
                    int nj, nk;
                    tie(nj, nk) = getExistent({inv1[j], inv2[j], inv1[k], inv2[k]});
                    if (deg == 1) {
                        used[nj] = true;
                        if (nj == i) {
                            break;
                        }
                    } else {
                        used[nj] = used[nk] = true;
                        if (nj == i || nk == i) {
                            int o = nj ^ i ^ nk;
                            if (inv1[o] != -1) {
                                cout << "0\n";
                                return 0;
                            }
                            add--;
                            break;
                        }
                    }
                    j = nj;
                    k = nk;
                } else {
                    int deg = (inv1[j] != -1) + (inv2[j] != -1);
                    int nj = inv1[j];
                    int nk = inv2[j];
                    if (deg > 2 || deg == 0) {
                        cout << "0\n";
                        return 0;
                    }
                    if (deg == 1) {
                        used[nj] = true;
                        if (nj == i) {
                            break;
                        }
                    } else {
                        used[nj] = used[nk] = true;
                        if (nj == i || nk == i) {
                            int o = nj ^ i ^ nk;
                            if (inv1[o] != -1) {
                                cout << "0\n";
                                return 0;
                            }
                            break;
                        }
                        add++;
                    }
                    j = nj;
                    k = nk;
                }
            }
            for (int i = 0; i < add; ++i) {
                ans += ans;
                ans %= MOD;
            }
        }
    }
    vector<int> sizes(n + 1, 0);
    for (int i = 0; i < n; ++i) {
        if (!used[i]) {
            int cnt = 0;
            for (int j = i; !used[j]; j = A[j]) {
                used[j] = true;
                cnt++;
            }
            sizes[cnt]++;
        }
    }
    for (int i = 1; i <= n; ++i) {
        if (sizes[i] != 0) {
            ans = (int64_t) ans * solve(sizes[i], i) % MOD;
        }
    }
    ans %= MOD;
    if (ans < 0) {
        ans += MOD;
    }
    cout << ans << '\n';
}

Measured memory_bytes: 3072