Problem:
Score : 1400 points

Problem Statement

You are given an integer sequence a of length N.
How many permutations p of the integers 1 through N satisfy the following condition?

For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.

Find the count modulo 10^9 + 7.

Constraints

1 ≤ N ≤ 10^5

a_i is an integer.

1 ≤ a_i ≤ N

Input

The input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.

Sample Input 1

3
1 2 3

Sample Output 1

4

The following four permutations satisfy the condition:

(1, 2, 3)

(1, 3, 2)

(3, 2, 1)

(2, 1, 3)

For example, (1, 3, 2) satisfies the condition because p_1 = 1, p_{p_2} = 2 and p_{p_3} = 3.

Sample Input 2

2
1 1

Sample Output 2

1

The following one permutation satisfies the condition:

(2, 1)

Sample Input 3

3
2 1 1

Sample Output 3

2

The following two permutations satisfy the condition:

(2, 3, 1)

(3, 1, 2)

Sample Input 4

3
1 1 1

Sample Output 4

0

Sample Input 5

13
2 1 4 3 6 7 5 9 10 8 8 9 11

Sample Output 5

6

Solution in C++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <cassert>
#define FOR(i, n, m) for(ll i = n; i < (int)m; i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define pb push_back
using namespace std;
using ll = std::int_fast64_t;
using P = pair<ll, ll>;
constexpr ll inf = 1000000000;
constexpr ll mod = 1000000007;
constexpr long double eps = 1e-15;
 
template<typename T1, typename T2>
ostream& operator<<(ostream& os, pair<T1, T2> p) {
    os << to_string(p.first) << " " << to_string(p.second);
    return os;
}
template<typename T>
ostream& operator<<(ostream& os, vector<T>& v) {
    REP(i, v.size()) {
        if(i) os << " ";
        os << to_string(v[i]);
    }
    return os;
}
 
struct modint {
    ll n;
public:
    modint(const ll n = 0) : n((n % mod + mod) % mod) {}
    static modint pow(modint a, int m) {
        modint r = 1;
        while(m > 0) {
            if(m & 1) { r *= a; }
            a = (a * a); m /= 2;
        }
        return r;
    }
    modint &operator++() { *this += 1; return *this; }
    modint &operator--() { *this -= 1; return *this; }
    modint operator++(int) { modint ret = *this; *this += 1; return ret; }
    modint operator--(int) { modint ret = *this; *this -= 1; return ret; }
    modint operator~() const { return (this -> pow(n, mod - 2)); } // inverse
    friend bool operator==(const modint& lhs, const modint& rhs) {
        return lhs.n == rhs.n;
    }
    friend bool operator<(const modint& lhs, const modint& rhs) {
        return lhs.n < rhs.n;
    }
    friend bool operator>(const modint& lhs, const modint& rhs) {
        return lhs.n > rhs.n;
    }
    friend modint &operator+=(modint& lhs, const modint& rhs) {
        lhs.n += rhs.n;
        if (lhs.n >= mod) lhs.n -= mod;
        return lhs;
    }
    friend modint &operator-=(modint& lhs, const modint& rhs) {
        lhs.n -= rhs.n;
        if (lhs.n < 0) lhs.n += mod;
        return lhs;
    }
    friend modint &operator*=(modint& lhs, const modint& rhs) {
        lhs.n = (lhs.n * rhs.n) % mod;
        return lhs;
    }
    friend modint &operator/=(modint& lhs, const modint& rhs) {
        lhs.n = (lhs.n * (~rhs).n) % mod;
        return lhs;
    }
    friend modint operator+(const modint& lhs, const modint& rhs) {
        return modint(lhs.n + rhs.n);
    }
    friend modint operator-(const modint& lhs, const modint& rhs) {
        return modint(lhs.n - rhs.n);
    }
    friend modint operator*(const modint& lhs, const modint& rhs) {
        return modint(lhs.n * rhs.n);
    }
    friend modint operator/(const modint& lhs, const modint& rhs) {
        return modint(lhs.n * (~rhs).n);
    }
};
istream& operator>>(istream& is, modint m) { is >> m.n; return is; }
ostream& operator<<(ostream& os, modint m) { os << m.n; return os; }
 
#define MAX_N 4040404
long long extgcd(long long a, long long b, long long& x, long long& y) {
    long long d = a;
    if (b != 0) {
        d = extgcd(b, a % b, y, x);
        y -= (a / b) * x;
    } else {
        x = 1; y = 0;
    }
    return d;
}
long long mod_inverse(long long a, long long m) {
    long long x, y;
    if(extgcd(a, m, x, y) == 1) return (m + x % m) % m;
    else return -1;
}
vector<long long> fact(MAX_N+1, inf);
long long mod_fact(long long n, long long& e) {
    if(fact[0] == inf) {
        fact[0]=1;
        if(MAX_N != 0) fact[1]=1;
        for(ll i = 2; i <= MAX_N; ++i) {
            fact[i] = (fact[i-1] * i) % mod;
        }
    }
    e = 0;
    if(n == 0) return 1;
    long long res = mod_fact(n / mod, e);
    e += n / mod;
    if((n / mod) % 2 != 0) return (res * (mod - fact[n % mod])) % mod;
    return (res * fact[n % mod]) % mod;
}
// return nCk
long long mod_comb(long long n, long long k) {
    if(n < 0 || k < 0 || n < k) return 0;
    long long e1, e2, e3;
    long long a1 = mod_fact(n, e1), a2 = mod_fact(k, e2), a3 = mod_fact(n - k, e3);
    if(e1 > e2 + e3) return 0;
    return (a1 * mod_inverse((a2 * a3) % mod, mod)) % mod;
}
 
using mi = modint;
 
mi mod_pow(mi a, ll n) {
    mi ret = 1;
    mi tmp = a;
    while(n > 0) {
        if(n % 2) ret *= tmp;
        tmp = tmp * tmp;
        n /= 2;
    }
    return ret;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector<int> a(n);
    REP(i, n) cin >> a[i];
    REP(i, n) a[i]--;
    vector<int> w(n, 1);
    vector<int> d(n, 0);
    vector<int> tmp(n, 0);
    REP(i, n) {
        d[a[i]]++;
        tmp[a[i]]++;
    }
    queue<int> q;
    REP(i, n) if(tmp[i] == 0) q.push(i);
    while(!q.empty()) {
        int p = q.front();
        q.pop();
        if(d[p] > 1) {
            cout << 0 << endl;
            return 0;
        }
        tmp[a[p]]--;
        w[a[p]] += w[p];
        if(tmp[a[p]] == 0) q.push(a[p]);
    }
    mi ans = 1;
    vector<int> c(n + 1, 0);
    REP(i, n) {
        if(tmp[i]) {
            deque<int> deq;
            int p = i, sum = 0;
            while(tmp[p]) {
                tmp[p] = 0;
                deq.push_back(w[p] - 1);
                sum += w[p] - 1;
                p = a[p];
            }
            reverse(ALL(deq));
            int m = (int)deq.size();
            
            /* OK */
            if(sum > m) {
                cout << 0 << endl;
                return 0;
            }
            
            /* OK */
            if(sum == 0) {
                c[m]++;
                continue;
            }
            
            /* OK */
            if(m == 1) {
                continue;
            }
            
            while(deq.front() == 0) {
                deq.push_back(deq.front());
                deq.pop_front();
            }
            if(sum == deq.front()) {
                if(sum == m) {
                    assert(false);
                    ans *= 1;
                }
                else ans *= 2;
                continue;
            }
            mi co = 1;
            int r = -1;
            for(int j = m - 1; j >= 0; j--) {
                if(deq[j] == 0) continue;
                if(r == -1) {
                    if(m - j > deq[j]) co *= 2;
                    if(m - j < deq[j]) {
                        cout << 0 << endl;
                        return 0;
                    }
                } else {
                    if(r - j > deq[j]) co *= 2;
                    if(r - j < deq[j]) {
                        cout << 0 << endl;
                        return 0;
                    }
                }
                r = j;
            }
            ans *= co;
        }
    }
    
    /* OK */
    vector<mi> fact(n + 1, 1);
    FOR(i, 1, n + 1) fact[i] = fact[i - 1] * i;
    FOR(i, 1, n + 1) {
        if(c[i] == 0) continue;
        mi co = 0;
        for(int j = 0; j <= c[i] / 2; j++) {
            mi tmp2 = mod_comb(c[i], 2 * j) * fact[2 * j];
            tmp2 *= mod_pow(fact[j] * mod_pow(2, j), mod - 2);
            tmp2 *= mod_pow(i, j);
            if(i % 2 && i != 1) tmp2 *= mod_pow(2, c[i] - 2 * j);
            co += tmp2;
        }
        ans *= co;
    }
    cout << ans << endl;
    return 0;
}