#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
const int mod = (int)1e9 + 7;

const int root = 646;

const int root_1 = 208611436;

const int root_pw = 1<<20;

int mult(int a, int b) {
    return (1LL * a * b) % mod;
}

int pw(int a, int b) {
    if (b == 0) return 1;
    if (b & 1) return mult(a, pw(a, b - 1));
    int res = pw(a, b / 2);
    return mult(res, res);
}

int sub(int a, int b) {
    int s = a - b;
    if (s < 0) s += mod;
    return s;
}

int sum(int a, int b) {
    int s = a + b;
    if (s >= mod) s -= mod;
    return s;
}

int n;
const int maxN = 2 * (int)1e5 + 10;
int a[maxN];
vector < int > g[maxN];
void zer() {
    cout << 0;
    exit(0);
}


int fact[maxN], invfact[maxN], inv[maxN];

void init() {
    inv[1] = invfact[1] = invfact[0] = fact[0] = fact[1] = 1;
    for (int i = 2; i < maxN; i++) {
        fact[i] = mult(fact[i - 1], i);
        inv[i] = mult(inv[mod % i], mod - mod / i);
        invfact[i] = mult(invfact[i - 1], inv[i]);
    }
}

int cnk(int n, int k) {
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return mult(fact[n], mult(invfact[k], invfact[n - k]));
}

bool used[maxN];
int solve(int cnt, int sz) {
    int val = 0;
    int coef = 1;
    for (int par = 0; 2 * par <= cnt; par++) {
        if (par > 0) coef = mult(sz, mult(coef, 2 * par - 1));
        if (sz % 2 == 0 || sz == 1) val = sum(val, mult(coef, cnk(cnt, 2 * par)));
        else {
            val = sum(val, mult(pw(2, cnt - 2 * par), mult(coef, cnk(cnt, 2 * par))));
        }
    }
    return val;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
//    freopen("input.txt", "r", stdin);
    init();
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        g[a[i]].emplace_back(i);
    }
    for (int i = 1; i <= n; i++) {
        if (g[i].size() > 2) {
            zer();
        }
    }
    for (int i = 1; i <= n; i++) {
        if (g[i].size() == 2 && (g[i][0] == i || g[i][1] == i)) {
            int other = g[i][0] + g[i][1] - i;
            if (g[other].size() > 0) zer();
            used[i] = used[other] = true;
        }
    }
    int ans = 1;
    for (int i = 1; i <= n; i++) {
        if (used[i]) continue;
        if (g[i].size() == 2) {
            used[i] = true;
            vector < int > cyc;
            int x = a[i];
            cyc.emplace_back(i);
            while (!used[x]) {
                cyc.emplace_back(x);
                used[x] = true;
                x = a[x];
            }
            if (x != i) zer();
            vector < pair < int, int > > all;
            for (int c = 0; c < cyc.size(); c++) {
                if (g[cyc[c]].size() > 1) {
                    int prv_cyc = cyc[(cyc.size() + c - 1) % cyc.size()];
                    int our_val = (g[cyc[c]][0] + g[cyc[c]][1] - prv_cyc);
                    used[our_val] = true;
                    int sz = 1;
                    while (g[our_val].size() > 0) {
                        if (g[our_val].size() > 1) zer();
                        our_val = g[our_val][0];
                        sz++;
                        used[our_val] = true;
                    }
                    all.emplace_back(sz, c);
                }
            }
            assert(!all.empty());
            for (int a = 0; a < all.size(); a++) {
                int from = all[a].second;
                int to = all[(a + 1) % all.size()].second;
                int his_sz = all[(a + 1) % all.size()].first;
                int len = 0;
                if (from == to) len = cyc.size();
                else {
                    if (from < to) len = to - from;
                    else {
                        len = (to + cyc.size() - from);
                    }
                }
                if (len > his_sz) ans = mult(ans, 2);
                else if (len < his_sz) zer();
            }

        }
    }
    vector < int > cnt(n + 1);
    for (int i = 1; i <= n; i++) {
        if (!used[i]) {
            if (g[i].size() != 1) zer();
            int pp = g[i][0];
            int cyc = 1;
            used[i] = true;
            while (pp != i) {
                cyc++;
                if (g[pp].size() != 1 || used[pp]) zer();
                used[pp] = true;
                pp = g[pp][0];
            }
            cnt[cyc]++;
        }
    }
    for (int i = 1; i <= n; i++) {
        if (cnt[i]) {
            ans = mult(ans, solve(cnt[i], i));
        }
    }
    cout << ans;
    return 0;
}

Measured memory_bytes: 11520