#include <algorithm>
#include <fstream>

//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 on C++.

int perm[100], input[100];

int main()
{
    std::ifstream in("sqroot.in");
    unsigned n;
    in >> n;
    for (unsigned i = 0; i < n; ++i)
    {
        in >> input[i];
        --input[i];
        perm[i] = i;
    }
    int ans = 0;
    do
    {
        bool ok = true;
        for (unsigned i = 0; ok && i < n; ++i)
        {
            if (perm[perm[i]] != input[i])
            {
                ok = false;
            }
        }
        if (ok)
        {
            ++ans;
        }
    }
    while (std::next_permutation(perm, perm + n));
    
    std::ofstream("sqroot.out") << ans;
    return 0;
}
