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

#define Gene template< class
#define Rics printer& operator,
Gene c> struct rge{c b, e;};
Gene c> rge<c> range(c i, c j){ return {i, j};}
struct printer{
    ~printer(){cerr<<endl;}
    Gene c >Rics(c x){ cerr<<boolalpha<<x; return *this;}
    Rics(string x){cerr<<x;return *this;}
    Gene c, class d >Rics(pair<c, d> x){ return *this,"(",x.first,", ",x.second,")";}
    Gene ... d, Gene ...> class c >Rics(c<d...> x){ return *this, range(begin(x), end(x));}
    Gene c >Rics(rge<c> x){
        *this,"["; for(auto it = x.b; it != x.e; ++it)
            *this,(it==x.b?"":", "),*it; return *this,"]";}
};
#define debug() cerr<<"LINE "<<__LINE__<<" >> ", printer()
#define dbg(x) "[",#x,": ",(x),"] "
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int my_rand(int l, int r) {
    return uniform_int_distribution<int>(l, r) (rng);
}

const int N = 1e5+100;
const int mod = 1e9+7;

int p[N];
int deg[N];
bool vis[N];
int fact[N], inv[N];
vector<int> g[N];

int bigmod(int b, int p) {
    if(p == 0) return 1;
    int t = bigmod(b, p/2);
    t = 1ll*t*t%mod;
    if(p & 1) return 1ll*b*t%mod;
    return t;
}

int main() {
//    freopen("in.txt", "r", stdin);
    ios::sync_with_stdio(0);
    cin.tie(0);


    fact[0] = inv[0] = 1;
    for(int i = 1; i < N; i++) {
        fact[i] = 1ll*fact[i-1]*i%mod;
        inv[i] = bigmod(fact[i], mod-2);
    }

    int n;
    cin >> n;
    for(int i = 1; i <= n; i++) {
        cin >> p[i];
        deg[p[i]]++;
        g[p[i]].push_back(i);
    }
    vector<int> que;
    for(int i = 1; i <= n; i++) {
        if(deg[i] == 0) {
            que.push_back(i);
        }
    }
    while(que.size() > 0) {
        int u = que.back();
        que.pop_back();
        deg[p[u]]--;
        if(deg[p[u]] == 0) {
            que.push_back(p[u]);
        }
    }
    vector<int> pure_cycle(n+1);
    int ans = 1;
    for(int i = 1; i <= n; i++) {
        if(deg[i] == 1 && vis[i] == false) {
            int cur = i;
            vector<int> cycle;
            while(vis[cur] == false) {
                vis[cur] = true;
                cycle.push_back(cur);
                cur = p[cur];
            }
            reverse(cycle.begin(), cycle.end());
            vector<int> bamboo((int)cycle.size());
            for(int j = 0; j < cycle.size(); j++) {
                int u = cycle[j];
                if(g[u].size() > 2) {
                    cout << "0" << endl;
                    exit(0);
                }
                if(g[u].size() == 1) continue;
//                debug(), dbg("hi");
                int rt = -1;
                for(int v : g[u]) {
                    if(vis[v] == false) {
                        rt = v;
                    }
                }
                assert(rt != -1);
                while(true) {
                    if(g[rt].size() > 1) {
                        cout << "0" << endl;
                        exit(0);
                    }
                    bamboo[j]++;
                    if(g[rt].size() == 0) break;
                    rt = g[rt][0];
                }
            }
            bool pure = true;
            for(int x : bamboo) {
                if(x) pure = false;
            }
            if(pure) {
//                debug(), dbg(bamboo);
//                debug(), dbg(cycle);
                pure_cycle[cycle.size()]++;
            }
            else {
                int sz = cycle.size();
                vector<int> bb;
                for(int j = 0; j < bamboo.size(); j++) {
                    if(bamboo[j] > 0) bb.push_back(j);
                }
                for(int j : bb) {
                    if(bamboo[j] > sz) {
                        cout << "0" << endl;
                        exit(0);
                    }
                    for(int k = 1; k < bamboo[j]; k++) {
                        if(bamboo[(j+k)%sz] > 0) {
                            cout << "0" << endl;
                            exit(0);
                        }
                    }
                    if(bamboo[(j+bamboo[j])%sz] == 0) {
                        ans *= 2;
                        ans %= mod;
                    }
                }
            }
        }
    }
//    debug(), dbg(ans);
    for(int sz = 1; sz <= n; sz++) {
        if(pure_cycle[sz] == 0) continue;
        int cnt = pure_cycle[sz];
        vector<int> dp(cnt+1);
        dp[0] = 1;
        int single = ((sz&1) && sz > 1 ? 2:1);
        dp[1] = single;
        for(int i = 2; i <= cnt; i++) {
            int eka = 1ll*dp[i-1]*single%mod;
            int doka = 1ll*(i-1)*sz%mod;
            doka = 1ll*doka*dp[i-2]%mod;
            dp[i] = (eka+doka)%mod;
        }
//        debug(), dbg(sz), dbg(cnt), dbg(single), dbg(dp);
        ans = 1ll*ans*dp[cnt]%mod;

//        int ways = 0;
//        int pw_2 = 1;
//        for(int k = 0; k * 2 <= cnt; k++) {
//            int cur_ways = 1ll*fact[cnt]*inv[cnt-2*k]%mod;
//            cur_ways = 1ll*cur_ways*inv[k]%mod;
//            cur_ways = 1ll*cur_ways*bigmod(pw_2, mod-2)%mod;
//            cur_ways = 1ll*cur_ways*bigmod(sz, k)%mod;
//            ways += cur_ways;
//            ways %= mod;
//            pw_2 *= 2;
//            pw_2 %= mod;
//        }
//        ans = 1ll*ans*ways%mod;
    }
    cout << ans << endl;
}





Measured memory_bytes: 12028