#include<bits/stdc++.h>
using namespace std;
const int N = 2e5+7, M = 1e9+7;

int power(int a, int p) {
    if (p == 0) return 1;
    int ans = power(a, p/2);
    ans = (1LL*ans * ans)%M;
    if (p%2)    ans = (1LL*ans*a)%M;
    return ans;
}

int fac[N], invfac[N], inv2;
void pre() {
    fac[0] = 1;
    for(int i=1; i<N; i++)  fac[i] = (1LL*i*fac[i-1])%M;
    invfac[N-1] = power(fac[N-1], M-2);
    for (int i=N-2; i>=0; i--)  invfac[i] = (1LL*invfac[i+1]*(i+1))%M;
    inv2 = (M+1)/2;
}

vector<int> in[N];
int a[N];
bool vis[N], vis2[N];
int cnt[N];

long long ans = 1;

void zero() {
    cout<<0<<endl;
    exit(0);
}

void make_comp(int u) {
    int cur = u;
    while (!vis[cur]) {
        vis[cur] = 1;
        cur = a[cur];
    }

    int z = cur;
    vector<int> cycle;
    cycle.push_back(z);

    while (a[cur] != z) {
        cur = a[cur];
        cycle.push_back(cur);
        assert(cycle.size() < N);
    }

    for (int x: cycle)  vis[x] = vis2[x] = 1;
    int k = cycle.size();

    vector<pair<int, int>> bamboos;
    for (int i=0; i<k; i++) {
        int x = cycle[i];
        if (in[x].size() == 1)  continue;

        int root = in[x][0], prv = in[x][1];
        if (vis2[root]) swap(root, prv);
        assert(!vis2[root] && vis2[prv]);

        int cur = root;
        int len = 0;
        while (true) {
            len++;
            vis[cur] = vis2[cur] = 1;
            if (in[cur].size() == 2)    zero();
            else if (in[cur].size() == 0)    {
                bamboos.push_back({i, len});
                break;
            }
            else {
                cur = in[cur][0];
                if (vis2[cur])  zero();
            }
        }
    }
    if (bamboos.empty())       {
        cnt[k]++;

//        cout<<"Comp: Cycle of length "<<k<<endl;
    }
    else {
        long long cur = 1;
        int sz = bamboos.size();
        for (int i=0; i<sz; i++) {
            int d = bamboos[i].first-bamboos[(i+sz-1)%sz].first;
            if (d <= 0) d+=k;
            int y = bamboos[i].second;
            if (y < d)      cur = (cur*2)%M;
            if (y > d)      cur = 0;
        }
        ans = (ans * cur)%M;

//        cout<<"Component of Length "<<k<<": Contrib = "<<cur<<"\t"<<"Bamboos: ";
//        for (auto pr: bamboos)  cout<<pr.first<<","<<pr.second<<"  ";
//        cout<<endl;
    }
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    pre();

    int n;
    cin>>n;

    for (int i=1; i<=n; i++) {
        cin>>a[i];
        in[a[i]].push_back(i);
        if (in[a[i]].size() > 2)    zero();
    }

    for (int i=1; i<=n; i++) {
        if (vis[i])     continue;
        make_comp(i);
    }

    for (int i=1; i<=n; i++) {
        if (cnt[i] == 0)    continue;

        int len = i;
        int m = cnt[i];

        long long cur = 0;

        for (int p=0; p*2<=m; p++) {
            long long ww = (1LL*fac[m]*invfac[m-2*p])%M;
            ww = (ww*invfac[p])%M;
            ww = (ww*power(inv2, p))%M;
            ww = (ww*power(len, p))%M;
            if (len%2 && len > 2)  ww = (ww*power(2, m-2*p));
            cur = (cur + ww)%M;
        }
//        cout<<m<<" Cycles of length "<<len<<": Contrib = "<<cur<<endl;
        ans = (ans * cur)%M;
    }
    cout<<ans<<endl;
}


Measured memory_bytes: 14044