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

typedef long long ll;
const ll mod = 1000*1000*1000+7;
ll po(ll x, ll n) {
    ll ret = 1;
    while(n) {
        if(n & 1) ret = ret * x % mod;
        x = x * x % mod;
        n /= 2;
    }
    return ret;
}
ll modinv(ll x) { return po(x, mod - 2); }
vector<ll> pp;
vector<ll> fact, invf;
ll comb(int n, int k) {
    return fact[n] * invf[k] % mod * invf[n - k] % mod;
}

int N;
vector<int> arr;
vector<vector<int> > inv;
vector<int> vis;
vector<vector<int> > cyc;
vector<int> sz;

vector<ll> cc;
ll dp(int n) {
    ll &ret = cc[n];
    if(ret != -1) return ret;
    if(n == 0) return ret = 1;
    else return ret = 1LL * (n - 1) * dp(n - 2) % mod;
}

int main() {
    pp.resize(100010);
    pp[0] = 1;
    for(int i = 1; i < 100010; i++) {
        pp[i] = pp[i - 1] * 2 % mod;
    }
    fact.resize(100010);
    fact[0] = 1;
    for(int i = 1; i < 100010; i++) {
        fact[i] = fact[i - 1] * i % mod;
    }
    invf.resize(100010);
    for(int i = 0; i < 100010; i++) {
        invf[i] = modinv(fact[i]);
    }

    scanf("%d", &N);
    //N = 99999;
    arr.resize(N);
    inv.resize(N);
    for(int i = 0; i < N; i++) {
        scanf("%d", &arr[i]);
        arr[i]--;
        //arr[i] = (i + 3) % N;
        inv[ arr[i] ].push_back(i);
    }

    for(int i = 0; i < N; i++) if(inv[i].size() > 2) {
        printf("0");
        return 0;
    }

    vis = vector<int>(N, 0);
    ll ans = 1;

    for(int i = 0; i < N; i++) if(inv[i].size() == 2 && arr[i] == i) {
        int x = inv[i][0] + inv[i][1] - i;
        if(inv[x].size() >= 1) {
            printf("0");
            return 0;
        }
        vis[i] = 1;
        vis[x] = 1;
    }

    for(int i = 0; i < N; i++) if(inv[i].size() == 2 && !vis[i]) {
        if(vis[ arr[i] ]) {
            printf("0");
            return 0;
        }
        
        cyc.clear();
        
        int u = arr[i];
        while(!vis[u]) {
            cyc.push_back(vector<int>(1, u));
            vis[u] = 1;
            u = arr[u];
        }
        if(u != arr[i]) {
            printf("0");
            return 0;
        }

        ll pans = 1;
        for(int i = (int)cyc.size() - 1; i >= 0; i--) {
            sort(cyc[i].begin(), cyc[i].end());
            cyc[i].resize(unique(cyc[i].begin(), cyc[i].end()) - cyc[i].begin());

            if(cyc[i].size() > 2) {
                printf("0");
                return 0;
            }

            int cnt = 0;
            for(int j = 0; j < cyc[i].size(); j++) {
                int x = cyc[i][j];
                for(int k = 0; k < inv[x].size(); k++) {
                    cyc[ (i + (int)cyc.size() - 1) % (int)cyc.size() ].push_back(inv[x][k]);
                    vis[ inv[x][k] ] = 1;
                    cnt++;
                }
            }

            if(cyc[i].size() == 2 && cnt == 1) pans=pans*2%mod;
        }
        sort(cyc.back().begin(), cyc.back().end());
        cyc.back().resize(unique(cyc.back().begin(), cyc.back().end()) - cyc.back().begin());

        if(cyc.back().size() > 2) {
            printf("0");
            return 0;
        }
        if(cyc.back().size() == 2 && inv[ cyc.back()[0] ].size() + inv[ cyc.back()[1] ].size() > 2) {
            printf("0");
            return 0;
        }
        ans = ans*pans%mod;
    }

    sz = vector<int>(N + 1, 0);

    for(int i = 0; i < N; i++) if(!vis[i]) {
        int u = i;
        int cnt = 0;
        while(!vis[u]) {
            vis[u] = 1;
            u = arr[u];
            cnt++;
        }
        if(u != i) {
            printf("0");
            return 0;
        }
        sz[cnt]++;
    }

    cc = vector<ll>(N + 1, -1);

    for(int i = 1; i <= N; i++) {
        ll pans = 0;
        for(int j = 0; j <= sz[i]; j += 2) {
            pans += comb(sz[i], j) * dp(j) % mod * po(i, j / 2) % mod * ((i != 1 && i % 2)? pp[ sz[i] - j ] : 1) % mod;
            pans %= mod;
        }
        ans = ans*pans%mod;
    }
    printf("%lld", ans);
}


Measured memory_bytes: 16048