OI VII - aut

// https://szkopul.edu.pl/problemset/problem/XtykYnwJUPeEgm7Kx9hLquHe/site/?key=statement

#include <bits/stdc++.h>

// #define GARY_DBG
#define GARY_LIB

constexpr int sizik = 10 * 1001;

#define ar std::array

typedef std::vector<std::vector<int>> _kra;

int n;
int p[sizik];
bool vis[sizik];
int cnt[sizik];

int fast_pow(int64_t a, int64_t b, int mod) {
    int64_t res = 1;
    a %= mod;
    while (b > 0) {
        if (b & 1) res = (res * a) % mod;
        a = (a * a) % mod;
        b >>= 1;
    }
    return (int)res;
}

void solve() {
    std::cin >> n;
    for (int i = 1; i <= n; i++) {
        std::cin >> p[i];
    }

    std::vector<int> lengths;
    bool isGood = true;

    for (int i = 1; i <= n; i++) {
        if (vis[i]) continue;
        int len = 0, cur = i;
        while (!vis[cur]) {
            vis[cur] = true;
            cur = p[cur];
            len++;
        }
        if (len % 2 == 0) isGood = false;
        if (cnt[len] == 0) lengths.push_back(len);
        cnt[len]++;
    }

    if (!isGood) {
        std::cout << "0\n";
        return;
    }

    int64_t cycles = 0;
    for (int i = 0; i < (int)lengths.size(); i++) {
        int L = lengths[i];
        cycles += 1LL * cnt[L] * (L / 2);
        cycles += 1LL * cnt[L] * (cnt[L] - 1) / 2 * L;

        for (int j = i + 1; j < (int)lengths.size(); j++) {
            int L2 = lengths[j];
            cycles += 1LL * cnt[L] * cnt[L2] * std::gcd(L, L2);
        }
    }

    std::cout << fast_pow(2, cycles, 1000) << '\n';
}

int32_t main() {
#ifndef GARY_DBG
    std::ios_base::sync_with_stdio(0);
    std::cin.tie(0);
    std::cout.tie(0);
#endif

    int t = 1;
    // std::cin >> t;

    for (; t > 0; t--) {
        solve();
    }

    return 0;
}