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

const int mod = 1e9 + 7;
const int MN = 100010;

int exp(int x, int n) {
    int ret = 1;
    while(n) {
        if(n & 1) ret = 1LL * ret * x % mod;
        x = 1LL * x * x % mod;
        n >>= 1;
    }
    return ret;
}
int inv(int x) {
    return exp(x, mod - 2);
}
int fact[MN], invf[MN], po[MN];
int comb(int n, int k) {
    if(n < k) return 0;
    return 1LL * fact[n] * invf[k] % mod * invf[n - k] % mod;
}

int N;
int A[MN];
vector<int> adj[MN];
int vis[MN], ccnt[MN];

int main() {
    fact[0] = 1;
    for(int i = 1; i < MN; i++) {
        fact[i] = 1LL * fact[i - 1] * i % mod;
    }
    for(int i = 0; i < MN; i++) {
        invf[i] = inv(fact[i]);
    }
    po[0] = 1;
    for(int i = 1; i < MN; i++) {
        po[i] = 1LL * po[i - 1] * 2 % mod;
    }

    scanf("%d", &N);

    for(int i = 0; i < N; i++) {
        scanf("%d", &A[i]);
        A[i]--;
    }

    for(int i = 0; i < N; i++) {
        adj[ A[i] ].push_back(i);
    }

    int ans = 1;
    for(int i = 0; i < N; i++) if(adj[i].size() >= 2 && !vis[i]) {
        vector<int> cycle, depth;
        int u = i;
        while(!vis[u]) {
            vis[u] = 1;
            cycle.push_back(u);
            u = A[u];
        }
        if(u != i) {
            printf("0");
            return 0;
        }

        int n = cycle.size();
        depth.resize(n);
        for(int j = 0; j < n; j++) {
            int u = cycle[j];
            int d = 0;
            while(1) {
                vis[u] = 1;
                vector<int> nxt;
                for(int k = 0; k < adj[u].size(); k++) {
                    int v = adj[u][k];
                    if(vis[v]) continue;
                    nxt.push_back(v);
                }
                if(nxt.size() >= 2) {
                    printf("0");
                    return 0;
                }
                if(nxt.size() == 0) break;
                u = nxt[0];
                d++;
            }
            depth[j] = d;
        }

        int la = -1;
        for(int j = 0; j < n; j++) if(depth[j]) la = j;
        for(int j = 0; j < n; j++) {
            if(depth[j]) {
                int len = (j + n - la) % n;
                if(!len) len += n;
                if(len < depth[j]) {
                    printf("0");
                    return 0;
                }
                if(len > depth[j]) {
                    ans = 1LL * ans * 2 % mod;
                }
                la = j;
            }
        }
    }
    for(int i = 0; i < N; i++) if(!vis[i]) {
        int u = i;
        int cnt = 0;
        while(!vis[u]) {
            vis[u] = 1;
            cnt++;
            u = A[u];
        }
        ccnt[cnt]++;
    }

    //cout << ans << endl;

    for(int i = 0; i < MN; i++) {
        if(!ccnt[i]) continue;

        //cout << i << ' ' << ccnt[i] << endl;

        int x = 0;
        int tmp = 1;
        for(int j = 0; j <= ccnt[i] / 2; j++) {
            int k = ccnt[i] - 2 * j;

            x += 1LL * tmp * invf[j] % mod * (i % 2 && i != 1? po[k] : 1) % mod * exp(i, j) % mod;
            x %= mod;
            tmp = 1LL * tmp * comb(ccnt[i] - 2 * j, 2) % mod;

            //cout << x << endl;
        }
        ans = 1LL * ans * x % mod;
    }
    printf("%d", ans);
}


Measured memory_bytes: 8568