#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <iomanip>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cstdint>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define rep(i,n) for(int i = 0; i < (int)(n); ++i)
#define all(x) (x).begin(),(x).end()

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}
 
template<class T> inline bool chmax(T &a, T b){
    if(a<b){
        a = b;
        return true;
    }
    return false;
}

template<class T> inline bool chmin(T &a, T b){
    if(a>b){
        a = b;
        return true;
    }
    return false;
}

class UnionFind {
private:
    int sz;
    vector<int> par, size_;
public:
    UnionFind(){}
    UnionFind(int node_size) : sz(node_size), par(sz), size_(sz, 1){
        iota(par.begin(), par.end(), 0);
    }
    int find(int x){
        if(par[x] == x) return x;
        else return par[x] = find(par[x]);
    }
    void unite(int x,int y){
        x = find(x), y = find(y);
        if(x == y) return;
        if(size_[x] < size_[y]) swap(x,y);
        par[y] = x;
        size_[x] += size_[y];
    }
    int size(int x){
        x = find(x);
        return size_[x];
    }
    bool same(int x,int y){
        return find(x) == find(y);
    }
};

template <unsigned int mod>
class ModInt {
private:
    unsigned int v;
    static unsigned int norm(const unsigned int& x){ return x < mod ? x : x - mod; }
    static ModInt make(const unsigned int& x){ ModInt m; return m.v = x, m; }
    static ModInt inv(const ModInt& x){ return make(inverse(x.v, mod)); }
    static unsigned int inverse(int a, int m){
        int u[] = {a, 1, 0}, v[] = {m, 0, 1}, t;
        while(*v){
            t = *u / *v;
            swap(u[0] -= t * v[0], v[0]), swap(u[1] -= t * v[1], v[1]), swap(u[2] -= t * v[2], v[2]);
        }
        return (u[1] % m + m) % m;
    }

public:
    ModInt() : v{0}{}
    ModInt(const long long val) : v{norm(val % mod + mod)} {}
    ModInt(const ModInt<mod>& n) : v{n()} {}
    explicit operator bool() const noexcept { return v != 0; }
    bool operator!() const noexcept { return !static_cast<bool>(*this); }
    ModInt& operator=(const ModInt& n){ return v = n(), (*this); }
    ModInt& operator=(const long long val){ return v = norm(val % mod + mod), (*this); }
    ModInt operator+() const { return *this; }
    ModInt operator-() const { return v == 0 ? make(0) : make(mod - v); }
    ModInt operator+(const ModInt& val) const { return make(norm(v + val())); }
    ModInt operator-(const ModInt& val) const { return make(norm(v + mod - val())); }
    ModInt operator*(const ModInt& val) const { return make((long long)v * val() % mod); }
    ModInt operator/(const ModInt& val) const { return *this * inv(val); }
    ModInt& operator+=(const ModInt& val){ return *this = *this + val; }
    ModInt& operator-=(const ModInt& val){ return *this = *this - val; }
    ModInt& operator*=(const ModInt& val){ return *this = *this * val; }
    ModInt& operator/=(const ModInt& val){ return *this = *this / val; }
    ModInt operator+(const long long val) const { return ModInt{v + val}; }
    ModInt operator-(const long long val) const { return ModInt{v - val}; }
    ModInt operator*(const long long val) const { return ModInt{(long long)v * (val % mod)}; }
    ModInt operator/(const long long val) const { return ModInt{(long long)v * inv(val)}; }
    ModInt& operator+=(const long long val){ return *this = *this + val; }
    ModInt& operator-=(const long long val){ return *this = *this - val; }
    ModInt& operator*=(const long long val){ return *this = *this * val; }
    ModInt& operator/=(const long long val){ return *this = *this / val; }
    bool operator==(const ModInt& val) const { return v == val.v; }
    bool operator!=(const ModInt& val) const { return !(*this == val); }
    bool operator==(const long long val) const { return v == norm(val % mod + mod); }
    bool operator!=(const long long val) const { return !(*this == val); }
    unsigned int operator()() const { return v; }
    friend ModInt operator+(const long long val, const ModInt& n) { return n + val; }
    friend ModInt operator-(const long long val, const ModInt& n) { return ModInt{val - n()}; }
    friend ModInt operator*(const long long val, const ModInt& n) { return n * val; }
    friend ModInt operator/(const long long val, const ModInt& n) { return ModInt{val} / n; }
    friend bool operator==(const long long val, const ModInt& n) { return n == val; }
    friend bool operator!=(const long long val, const ModInt& n) { return !(val == n); }
    friend istream& operator>>(istream& is, ModInt& n){
        unsigned int v;
        return is >> v, n = v, is;
    }
    friend ostream& operator<<(ostream& os, const ModInt& n){ return (os << n()); }
    friend ModInt mod_pow(ModInt x, long long n){
        ModInt ans = 1;
        while(n){
            if(n & 1) ans *= x;
            x *= x, n >>= 1;
        }
        return ans;
    }
};

#define MOD 1000000007
using mod = ModInt<MOD>;

#define MAX_N 200010
mod inv[MAX_N],fac[MAX_N],finv[MAX_N];
// mod f[MAX_N];
void make()
{
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for(int i=2;i<MAX_N;i++){
        inv[i] = MOD - inv[MOD % i] * (MOD / i);
        fac[i] = fac[i-1] * i;
        finv[i] = finv[i-1] * inv[i];
    }
    // f[0] = 1;
    // for(int i=0;i<MAX_N-1;i++){
    //     if(i+3<MAX_N){
    //         f[i+3] += f[i];
    //     }
    //     f[i+1] += f[i];
    // }
}

mod comb(int a, int b)
{
    if(a<b) return 0;
    return fac[a] * finv[b] * finv[a-b];
}

mod calc(vector<int>&a,vector<int>&p){
    for(auto x:p){
        if(a[x]==x)return 0;
    }
    mod res = 0;
    int id = p[0];
    for(int i=0;i<=2*(p.size());i++){
        id = a[id];
    }
    vector<int> v;
    set<int> st;
    st.insert(id);
    map<int,int> inv;
    int ppp = id;
    while(a[id]!=ppp){
        v.push_back(a[id]);
        inv[a[id]] = id;
        id = a[id];
        st.insert(id);
    }
    inv[a[id]] = id;
    st.insert(a[id]);
    st.insert(id);
    v.push_back(a[id]);
    reverse(v.begin(),v.end());
    map<int,int> cnt;
    map<int,int> s;
    for(auto x:p){
        cnt[a[x]]++;
    }
    for(auto x:p){
        int ss = x;
        if(cnt[ss]==0){
            int c = 0;
            while(st.count(ss)==0){
                c++;
                ss = a[ss];
            }
            if(s[ss]!=0){
                return 0;
            }
            s[ss] = c;
        }
    }
    bool flag = 1;
    for(auto x:v){
        if(s[x]!=0){
            flag = 0;
        }
    }
    if(flag){
        int n = p.size();
        if(n%2==1){
            return 2;
        }
        return 1;
    }else{
        int x = id;
        int n = v.size();
        map<int,bool> bl;
        bool ng = 0;
        for(int i=0;i<=2*n;i++){
            if(s[x]!=0){
                int b = s[x];
                int y = x;
                rep(zz,b){
                    if(zz!=0){
                        if(s[y]!=0){
                            ng = 1;
                        }
                    }
                    bl[y] = 1;
                    y = inv[y];
                }
            }
            x = inv[x];
        }
        if(ng)return 0;
        x = id;
        int start;
        for(int i=0;i<=2*n;i++){
            if(bl[x]){
                start = x;
                break;
            }
            x = inv[x];
        }
        int ccc = 0;
        vector<int> L;
        for(int i=0;i<n;i++){
            if(bl[start]){
                if(ccc!=0){
                    L.push_back(ccc);
                    ccc = 0;
                }
            }else{
                ccc++;
            }
            start = inv[start];
        }
        if(ccc!=0){
            L.push_back(ccc);
        }
        mod res = 1;
        
        for(auto y:L){
            // cerr << "test: " << y << endl;
            res *= 2;
            
        }
        return res;
    }
}

int main(){
    make();
    int n;
    cin >> n;
    vector<int> a(n);
    UnionFind uf(n);
    rep(i,n){
        cin >> a[i];
        a[i]--;
        uf.unite(i,a[i]);
    }
    vector<vector<int> > g(n);
    rep(i,n){
        g[uf.find(i)].push_back(i);
    }
    mod res = 1;
    int c = 0;
    int d = 0;
    vector<int> ppp(n+1);
    rep(i,n){
        if(g[i].size()==0)continue;
        if(g[i].size()==1){
            c++;
        }else if(g[i].size()==2){
            int x = g[i][0];
            int y = g[i][1];
            if(a[x]==y&&a[y]==x){
                d++;
            }
        }else{
            map<int,int> mp;
            for(auto x:g[i]){
                mp[a[x]]++;
            }
            bool flag = 1;
            for(auto x:g[i]){
                if(mp[x]!=1)flag = 0;
            }
            if(flag){
                ppp[g[i].size()]++;
            }else{
                res *= calc(a,g[i]);   
            }
        }
    }
    if(d!=0){
        mod tmp = 0;
        for(int x=0;x<=(d/2);x++){
            tmp += comb(d,2*x)*fac[2*x]*finv[x];
        }
        res *= tmp;
    }
    if(c!=0){
        mod tmp = 0;
        mod inv = 1;
        for(int x=0;x<=(c/2);x++){
            tmp += comb(c,2*x)*fac[2*x]*finv[x]*inv;
            inv /=2;
        }
        res *= tmp;
    }
    cerr << res << endl;
    rep(i,n+1){
        if(ppp[i]!=0){        
            mod tmp = 0;
            mod inv = 1;
            mod A = 1; 
            if(i%2==1){
                A = 2;
            }
            c = ppp[i];
            cerr << i << " " <<  A << " " << c << endl;
            for(int x=0;x<=(c/2);x++){
                tmp += comb(c,2*x)*fac[2*x]*finv[x]*inv * mod_pow(A,c-2*x);
                inv /=(mod)2;
                inv *= (mod)i;
            }
            res *= tmp;
        }
    }
    cout << res << endl;
    return 0;
}

Measured memory_bytes: 35448