#include<bits/stdc++.h>

using namespace std;

int ans, N, frq[100009], fac[100009], inv[100009], ap[100009], a[100009], cyc[100009], foot_length[100009];
const int mod = 1e9 + 7;
vector < int > h[100009];

int add (int x, int y) {int ans = x + y; if (ans >= mod) ans -= mod; return ans;}
int subtract (int x, int y) {if (x >= y) return x - y; return x - y + mod;}
int mul (int x, int y) {return 1LL * x * y % mod;}
void adto (int &x, int y) {x += y; if (x >= mod) x -= mod;}

int power (int a, int b)
{
    int p = 1;
    for (int i=0; (1<<i) <= b; i++)
    {
        if (b & (1 << i)) p = mul (p, a);
        a = mul (a, a);
    }
    return p;
}

void Prec (int lim){fac[0] = inv[0] = 1;for (int i=1; i<=lim; i++)fac[i] = mul (fac[i - 1], i);
inv[lim] = power (fac[lim], mod - 2);for (int i=lim - 1; i>=1; i--)inv[i] = mul (inv[i + 1], i + 1);}
int comb (int N, int K){int ans = mul (fac[N], inv[N - K]);ans = mul (ans, inv[K]);return ans;}

void Ans (int val)
{
    printf ("%d\n", val);
    exit (0);
}

int main ()
{
//freopen ("input", "r", stdin);
//freopen ("output", "w", stdout);

scanf ("%d", &N), ans = 1, Prec (N);
for (int i=1; i<=N; i++)
    scanf ("%d", &a[i]), h[a[i]].push_back (i);
for (int i_init=1; i_init<=N; i_init++)
    if (ap[i_init] == 0)
    {
        int j = i_init;
        while (ap[j] == 0)
            ap[j] = 1, j = a[j];
        int k = j, sz = 0;
        while (ap[k] == 1)
            cyc[++sz] = k, ap[k] = 2, k = a[k];
        bool simpleCycle = 1;
        for (int i=1; i<=sz; i++)
        {
            int curr = cyc[i]; foot_length[i] = 0;
            while (1)
            {
                int cnt = 0, prv = -1;
                for (auto it : h[curr])
                    if (ap[it] != 2)
                        prv = it, cnt ++;
                if (cnt == 0) break;
                if (cnt > 1) Ans (0);
                curr = prv, foot_length[i] ++, ap[curr] = 2;
            }
            simpleCycle &= (foot_length[i] == 0);
        }
        if (simpleCycle) frq[sz] ++;
        else
        {
            vector < pair < int, int > > feet;
            for (int i=1; i<=sz; i++)
                if (foot_length[i] > 0)
                    feet.push_back ({i, foot_length[i]});
            for (int i=1; i<feet.size (); i++)
            {
                int d = feet[i].first - feet[i - 1].first, l = feet[i].second;
                if (l > d) Ans (0);
                if (l < d) ans = mul (ans, 2);
            }
            int d = feet[0].first + sz - feet[feet.size () - 1].first, l = feet[0].second;
            if (l > d) Ans (0);
            if (l < d) ans = mul (ans, 2);
        }
    }
for (int i=1; i<=N; i++)
    if (frq[i] > 0)
    {
        int base = (i & 1) + 1 - (i == 1);///how many ways to get this cycle from just one other cycle of the same length
        int curr = 1, no_ways = 0;
        for (int j=0; 2 * j <= frq[i]; j++)
        {
            ///I've already picked the pairs and their way of matching
            int realCurr = mul (curr, inv[j]);
            adto (no_ways, mul (realCurr, power (base, frq[i] - 2 * j)));
            ///
            if (2 * (j + 1) <= frq[i])
                curr = mul (curr, comb (frq[i] - 2 * j, 2)), curr = mul (curr, i);///moving forward to the next step
        }
        ans = mul (ans, no_ways);
    }
Ans (ans);
return 0;
}


Measured memory_bytes: 8064