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

//Correct solution.
//Does not use big integer division.

public class sqroot_mb {
    public static void main(String[] args) throws IOException {
         Scanner in = new Scanner(new File("sqroot.in"));
         int n = in.nextInt();
         assert 1 <= n && n <= 100 : "N is out of bounds";
         
         int[] perm = new int[n];
         boolean[] uu = new boolean[n];
         for (int i = 0; i < n; ++i) {
             perm[i] = in.nextInt() - 1;
             assert 0 <= perm[i] && perm[i] < n : "Illegal permutation member (out of bounds)";
             assert !uu[perm[i]] : "Illegal permutation member (duplicate)";
             uu[perm[i]] = true;
         }
         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 ? 1 : 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]]).multiply(big[i].pow(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].multiply(big[i].pow(j / 2)).multiply(C[cycleCount[i]][j]));
                     }
                     answer = answer.multiply(mul);
                 }
             }
         }

         PrintWriter out = new PrintWriter("sqroot.out");
         out.println(answer);
         out.close();
    }
}
