#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;

const int MOD = 1e9 + 7;

long long ModPow(long long x, long long n, long long m) {
        long long res = 1;
        while (n > 0) {
                if (n & 1) res = res * x % m;
                x = x * x % m;
                n >>= 1;
        }
        return res;
}

long long Inv(long long a) { return ModPow(a, MOD - 2, MOD); }

const int N = 500000;
long long fact[N];
long long invfact[N];
void init() {
        fact[0] = 1;
        for (int i = 1; i < N; i ++) fact[i] = fact[i - 1] * i % MOD;
        for (int i = 0; i < N; i ++) invfact[i] = Inv(fact[i]);
}
long long C(long long n, long long r) {
        if (n < 0 || r < 0 || n < r) return 0;
        return fact[n] * invfact[n - r] % MOD * invfact[r] % MOD;
}

int main() {
        init();
        int n;
        scanf("%d", &n); 
        vector<int> used(n, -1), a(n);
        vector<vector<int>> rev(n);
        for (int i = 0; i < n; i ++) {
                scanf("%d", &a[i]);
                a[i] --;
                rev[a[i]].push_back(i);
        }
        vector<int> simple(n + 1);
        vector<bool> on_cycle(n);
        for (int i = 0; i < n; i ++) if (used[i] == -1) {
                int now = i;
                while (true) {
                        if (used[now] == i) {
                                on_cycle[now] = true;
                                break;
                        }
                        if (used[now] != -1) break;
                        used[now] = i;
                        now = a[now];
                }
        }
        long long ans = 1;
        for (int i = 0; i < n; i ++) if (on_cycle[i]) {
                vector<int> cycle;
                int now = i;
                do {
                        cycle.push_back(now);
                        now = a[now];
                } while (now != i);
                int cyclecnt = cycle.size();
                vector<int> branch(cyclecnt);
                for (int j = 0; j < cyclecnt; j ++) {
                        if (rev[cycle[j]].size() >= 3) return !printf("0\n");
                        if (rev[cycle[j]].size() == 2) {
                                int q = rev[cycle[j]][0] + rev[cycle[j]][1] - cycle[(j + cyclecnt - 1) % cyclecnt];
                                int len = 1;
                                while (true) {
                                        if (rev[q].size() >= 2) return !printf("0\n");
                                        if (rev[q].size() == 1) {
                                                len ++;
                                                q = rev[q][0];
                                                continue;
                                        }
                                        break;
                                }                                           
                                branch[j] = len;
                        }
                }
                for (int j = 0; j < cyclecnt; j ++) branch.push_back(branch[j]);
                int last = -1;
                for (int j = 0; j < cyclecnt * 2; j ++) {
                        if (branch[j] > 0) {
                                if (j >= cycle.size()) {
                                        int gap = j - last;
                                        if (gap < branch[j]) return !printf("0\n");
                                        else if (gap == branch[j]) ;//nothing
                                        else ans = ans * 2 % MOD;     
                                }
                                last = j;
                        }
                }
                if (last == -1) simple[cyclecnt] ++;
        }
        for (int i = 1; i <= n; i ++) {
                int cnt = simple[i];
                if (cnt == 0) continue;
                long long res = 0, cur = 1;
                int w = (i == 1 || i % 2 == 0) ? 1 : 2;
                for (int j = 0; j <= cnt / 2; j ++) {
                        res = (res + cur * ModPow(w, cnt - 2 * j, MOD) % MOD * invfact[j] % MOD * ModPow(i, j, MOD)) % MOD;
                        cur = cur * C(cnt - 2 * j, 2) % MOD;
                }
                ans = ans * res % MOD;
        }
        printf("%lld\n", ans);
}


Measured memory_bytes: 15992