#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;

#define forn(i, a, n) for (int i = a; i < n; ++i)
#define ford(i, a, n) for (int i = n - 1; i >= a; --i)
#define fore(i, a, n) for (int i = a; i <= n; ++i)
#define all(a) (a).begin(), (a).end()
#define fs first
#define sn second
#define trace(a)\
    for (auto i : a) cerr << i << ' ';\
    cerr << '\n'
#define eb emplace_back

#ifndef M_PI
const ld M_PI = acos(-1.0);
#endif

const ld eps = 1e-9;
const int INF = 2000000000;
const ll LINF = 1ll * INF * INF;
const ll MOD = 1000000007;

const ll HALF = (MOD + 1) / 2;

void fail() {
    cout << "0\n";
    exit(0);
}

ll Pow(ll n, ll k) {
    ll x = 1, y = n;
    while (k) {
        if (k % 2) x = x * y % MOD;
        y = y * y % MOD;
        k /= 2;
    }
    return x;
}

ll Inv(ll n) {
    return Pow(n, MOD - 2);
}

const int N = 1e5 + 10;
ll fact[N];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    fact[0] = 1;
    fore(i, 1, n) fact[i] = fact[i - 1] * i % MOD;
    vector<int> p(n);
    forn(i, 0, n) {
        cin >> p[i];
        --p[i];
    }
    vector<int> cycle(n, -2);
    int cycle_now = 0;
    vector<int> u(n);
    forn(i, 0, n) if (!u[i]) {
        int j = i;
        while (!u[j]) {
            u[j] = 1;
            j = p[j];
        }
        if (cycle[j] == -2) {
            while (cycle[j] == -2) {
                cycle[j] = cycle_now;
                j = p[j];
            }
            ++cycle_now;
        }
        j = i;
        while (cycle[j] == -2) {
            cycle[j] = -1;
            j = p[j];
        }
    }
    //trace(cycle);
    vector<int> indeg(n);
    forn(i, 0, n) indeg[p[i]]++;
    forn(i, 0, n) {
        if (indeg[i] > 2) fail();
        if (indeg[i] == 2 && cycle[i] == -1) fail();
    }
    vector<int> path_len(n);
    forn(i, 0, n) if (indeg[i] == 0) {
        int j = i, cnt = 0;
        while (cycle[j] == -1) {
            ++cnt;
            j = p[j];
        }
        path_len[j] = cnt;
    }
    ll ans = 1;
    vector<int> pure_cycles(n + 1);
    vector<char> uc(cycle_now);
    forn(i, 0, n) if (cycle[i] != -1 && !uc[cycle[i]]) {
        uc[cycle[i]] = 1;
        bool pure = 1;
        int len = 0, j = i;
        do {
            if (indeg[j] > 1) {
                assert(indeg[j] == 2);
                pure = 0;
                break;
            }
            ++len;
            j = p[j];
        } while (j != i);
        if (pure) {
            pure_cycles[len]++;
            continue;
        }
        int st = j;
        do {
            int len_btw = 0;
            do {
                ++len_btw;
                j = p[j];
            } while (indeg[j] == 1);
            --len_btw;
            if (len_btw >= path_len[j]) {
                // 2 ways
                ans *= 2;
                if (ans >= MOD) ans -= MOD;
                //cerr << i << ' ' << j << ' ' << len_btw << ' ' << path_len[j] << '\n';
            } else if (len_btw + 1 == path_len[j]) {
                // 1 way
            } else fail();
        } while (j != st);
    }
    fore(i, 0, n) {
        ll loc = 0;
        fore(j, 0, pure_cycles[i] / 2) {
            ll add = 1;
            add *= fact[pure_cycles[i]];
            add %= MOD;
            add *= Inv(fact[pure_cycles[i] - 2 * j]);
            add %= MOD;
            add *= Inv(fact[j]);
            add %= MOD;
            add *= Pow(HALF, j);
            add %= MOD;
            add *= Pow(i, j);
            add %= MOD;
            if (i % 2 && i > 1) {
                add *= Pow(2, pure_cycles[i] - 2 * j);
                add %= MOD;
            }
            //if (j && add) cerr << i << ' ' << j << ' ' << add << '\n';
            loc += add;
            if (loc >= MOD) loc -= MOD;
        }
        //if (loc != 1) cerr << i << ' ' << loc << '\n';
        ans *= loc;
        ans %= MOD;
    }
    cout << ans << '\n';
}


Measured memory_bytes: 3456