#include <cstdio>
#include <vector>
using namespace std;

const int MOD = 1e9 + 7;

int main() {
        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 ++) {
                if (simple[i] > 0) {
                        int all = simple[i];
                        vector<long long> dp(all + 1);
                        dp[0] = 1;
                        for (int j = 1; j <= all; j ++) {
                                if (i > 1 && i % 2 == 1) dp[j] = dp[j - 1] * 2 % MOD;
                                else dp[j] = dp[j - 1];
                                if (j > 1) dp[j] = (dp[j] + dp[j - 2] * (j - 1) % MOD * i % MOD) % MOD;
                        }
                        ans = ans * dp[all] % MOD;
                }
        }
        printf("%lld\n", ans);
}


Measured memory_bytes: 8184