import java.io.*;
import java.util.*;
import java.math.BigInteger;

//Problem "Square root of a permutation".
//14th Personal Internet Olympiad, season 2007-2008.
//Problem Author: Maxim Buzdalov, buzdalov@rain.ifmo.ru
//Solution Author: Maxim Buzdalov, buzdalov@rain.ifmo.ru

//Solution with bug in formula.

public class sqroot_mb_bug1 {
    public static void main(String[] args) throws IOException {
        Scanner in = new Scanner(new File("sqroot.in"));
        int n = in.nextInt();
        int[] perm = new int[n];
        for (int i = 0; i < n; ++i) {
            perm[i] = in.nextInt() - 1;
        }
        int[] cycleCount = new int[n + 1];
        boolean[] used = new boolean[n];
        for (int i = 0; i < n; ++i) {
            if (!used[i]) {
                int cl = 0;
                int j = i;
                do {
                    ++cl;
                    used[j] = true;
                    j = perm[j];
                } while (!used[j]);
                cycleCount[cl]++;
            }
        }
        
        BigInteger[] big = new BigInteger[n + 1];
        for (int i = 0; i <= n; ++i) {
            big[i] = BigInteger.valueOf(i);
        }
        
        BigInteger[] fact = new BigInteger[n + 1];
        BigInteger[] divFact = new BigInteger[n + 1];
        divFact[0] = BigInteger.ONE;
        fact[0] = BigInteger.ONE;
        for (int i = 1; i <= n; ++i) {
            divFact[i] = divFact[i - 1].multiply(big[i % 2 == 0 ? i / 2 : i]);
            fact[i] = fact[i - 1].multiply(big[i]);
        }
        
        BigInteger[][] C = new BigInteger[n + 1][];
        for (int i = 0; i <= n; ++i) {
            C[i] = new BigInteger[i + 1];
            C[i][0] = C[i][i] = BigInteger.ONE;
            for (int j = 1; j < i; ++j) {
                C[i][j] = C[i - 1][j].add(C[i - 1][j - 1]);
            }
        }
        
        BigInteger answer = BigInteger.ONE;
        for (int i = 1; answer.signum() > 0 && i <= n; ++i) {
            if (cycleCount[i] > 0) {
                if (i % 2 == 0) {
                    if (cycleCount[i] % 2 == 0) {
                        answer = answer.multiply(divFact[cycleCount[i]]).divide(fact[cycleCount[i] / 2]);
                    } else {
                        answer = BigInteger.ZERO;
                    }
                } else {
                    BigInteger mul = BigInteger.ONE;
                    for (int j = 2; j <= cycleCount[i]; j += 2) {
                        mul = mul.add(divFact[j].divide(fact[j / 2]).multiply(C[cycleCount[i]][j]));
                    }
                    answer = answer.multiply(mul);
                }
            }
        }
        
        PrintWriter out = new PrintWriter("sqroot.out");
        out.println(answer);
        out.close();
    }
}
