import java.io.*;
import java.util.*;

//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

//Brute force solution.

public class sqroot_mb_brute {
    static boolean next(int[] perm) {
        int n = perm.length;
        for (int i = n - 2; i >= 0; --i) {
            if (perm[i] < perm[i + 1]) {
                int j = i + 1;
                for (; j < n; ++j) {
                    if (perm[j] < perm[i]) {
                        break;
                    }
                }
                --j;
                int t = perm[i]; 
                perm[i] = perm[j]; 
                perm[j] = t;
                for (int k = i + 1, l = n - 1; k < l; ++k, --l) {
                    t = perm[k];
                    perm[k] = perm[l];
                    perm[l] = t;
                }
                return true;
            }
        }
        return false;
    }
    
    public static void main(String[] args) throws IOException {
        Scanner in = new Scanner(new File("sqroot.in"));
        int n = in.nextInt();
        int[] input = new int[n];
        int[] perm = new int[n];
        for (int i = 0; i < n; ++i) {
            input[i] = in.nextInt() - 1;
            perm[i] = i;
        }
        in.close();
        
        int count = 0;
        do {
            boolean good = true;
            for (int i = 0; good && i < n; ++i) {
                if (perm[perm[i]] != input[i]) {
                    good = false;
                }
            }
            if (good) {
                ++count;
            }
        } while (next(perm));
        
        PrintWriter out = new PrintWriter("sqroot.out");
        out.println(count);
        out.close();
    }
}
