#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
const int N = 2e5 + 7, MOD = 1000 * 1000 * 1000 + 7;

int mod(int n) {
    n %= MOD;
    if (n < 0) return n + MOD;
    else return n;
}   
int fp(int a, int p) {
    int ans = 1, c = a;
    for (int i = 0; (1ll << i) <= p; ++i) {
        if ((p >> i) & 1) ans = mod(ans * c);
        c = mod(c * c);
    }   
    return ans;
}   
int dv(int a, int b) { return mod(a * fp(b, MOD - 2)); }
int f[N], inv[N];
int C(int n, int k) {
    return mod(f[n] * mod(inv[k] * inv[n - k]));
}   
void calc_f() {
    f[0] = 1;
    for (int i = 1; i < N; ++i) 
        f[i] = mod(f[i - 1] * i);
    inv[N - 1] = fp(f[N - 1], MOD - 2);
    for (int i = N - 2; i >= 0; --i)
        inv[i] = mod(inv[i + 1] * (i + 1));
}   

int a[N], in[N], sub[N];
bool onc[N];
bool used[N];

int solve(vector <int> a) {
    int n = a.size();
    /*
    int sum = 0;
    for (auto e : a)
        sum += e;

    if ((n & 1) && sum == 0)
        return 2;
    */
    int ans = 1;
    for (int i = 0; i < a.size(); ++i) {
        if (!a[i])
            continue;
        int f = 0;
        for (int sh = 0; sh < 2 && sh + a[i] <= n; ++sh) {
            bool ban = 0;
            for (int j = 0; j < a[i]; ++j) {
                int p = (i - sh - j + n + n) % n;
                if (p != i && a[p]) {
                    ban = 1;
                }
            }   
            f += !ban;
        }   
        ans = mod(ans * f);
    }   
    return ans;
}   
int size(vector <int> a) {
    int ans = a.size();
    for (auto e : a)
        ans += e;
    return ans;
}   

int cnt[N];

int p[N];
/*
int pairing1(int n) {
    int ans = 0;
    for (int i = 0; i * 2 <= n; ++i) {
        ans = mod(ans + C(n, i * 2) * p[i * 2]);                        
    }   
    return ans;
}   
int pairing2(int n) {
    p[0] = 1;
    for (int i = 2; i < N; i += 2)
        p[i] = mod(p[i - 2] * (i - 1));
    int ans = 0;
    for (int i = 0; i * 2 <= n; ++i) {
        ans = mod(ans + mod(C(n, i * 2) * p[i * 2]) * fp(2, i));                        
    }   
    return ans;
}   
int pairing3(int n) {
    p[0] = 1;
    for (int i = 2; i < N; i += 2)
        p[i] = mod(p[i - 2] * (i - 1));
    int ans = 0;
    for (int i = 0; i * 2 <= n; ++i) {
        ans = mod(ans + mod(C(n, i * 2) * p[i * 2]) * mod(fp(2, n - 2 * i) * fp(3, i)));                        
    }   
    return ans;
}
*/
int pairing(int len, int n) {
    int ans = 0;
    for (int i = 0; i * 2 <= n; ++i) {
        int add = mod(mod(C(n, i * 2) * p[i * 2]) * fp(len, i));
        if (len % 2 == 1 && len > 1)
            add = mod(add * fp(2, n - 2 * i));
        ans = mod(ans + add);
    }   
    return ans;
}     

signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    #define endl '\n'
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif

    p[0] = 1;
    for (int i = 2; i < N; i += 2)
        p[i] = mod(p[i - 2] * (i - 1));

    calc_f();

    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        ++in[a[i]];
    }   
    queue <int> q;
    for (int i = 1; i <= n; ++i) {
        onc[i] = 1;
        if (!in[i]) {
            q.push(i);                                                
        }   
    }   
    while (q.size()) {
        int u = q.front();
        q.pop();
        onc[u] = 0;
        --in[a[u]];
        sub[a[u]] += sub[u] + 1;
        if (!in[a[u]]) {
            q.push(a[u]);
        }   
    }   

    memset(in, 0, sizeof in);
    for (int i = 1; i <= n; ++i) {
        ++in[a[i]];
    }   
    for (int i = 1; i <= n; ++i) {
        if (!onc[i] && in[i] > 1) {
            cout << 0 << endl;
            exit(0);
        }
        if (in[i] > 2) {
            cout << 0 << endl;
            exit(0);
        }   
    }   

    vector <vector <int> > c;
    for (int i = 1; i <= n; ++i) {
        if (!used[i] && onc[i]) {
            int u = i;
            c.app({});
            while (!used[u]) {
                used[u] = 1;
                c.back().app(sub[u]);
                u = a[u];
            }   
        }
    }   
    
    #ifdef HOME
    for (auto e : c) {
        cout << "cycle : ";
        for (auto x : e)
            cout << x << ' ';
        cout << endl;
    }   
    #endif

    int ans = 1;        
    for (auto e : c) {
        if (e.size() == 1 && e[0] > 1) {
            cout << 0 << endl;
            exit(0);
        }   
        
        int sum = 0;
        for (auto x : e)
            sum += x;

        if (sum) {
            ans = ans * solve(e) % MOD;
        }   
        else {
            int len = e.size();
            ++cnt[len];
        }   
    }   
    
    #ifdef HOME
    cout << "big " << ans << endl;
    #endif

    for (int i = 1; i <= n; ++i) {
        ans = ans * pairing(i, cnt[i]) % MOD;
    }   
    cout << ans << endl;
}

Measured memory_bytes: 16560