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: Time Limit Exceeded)

// Copyright (C) 2017 __debug.

// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; version 3

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; If not, see <http://www.gnu.org/licenses/>.


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>

#define x first
#define y second
#define MP std::make_pair
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(), (x).end()
#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#ifdef __linux__
#define getchar getchar_unlocked
#define putchar putchar_unlocked
#endif

using std::pair;
using std::vector;
using std::string;

typedef long long LL;
typedef pair<int, int> Pii;

const int oo = 0x3f3f3f3f;

template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, true : false; }
template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, true : false; }
string procStatus()
{
    std::ifstream t("/proc/self/status");
    return string(std::istreambuf_iterator<char>(t), std::istreambuf_iterator<char>());
}
template<typename T> T read(T &x)
{
    int f = 1;
    char ch = getchar();
    for (; !isdigit(ch); ch = getchar())
        f = (ch == '-' ? -1 : 1);
    for (x = 0; isdigit(ch); ch = getchar())
        x = 10 * x + ch - '0';
    return x *= f;
}
template<typename T> void write(T x)
{
    if (x == 0) {
        putchar('0');
        return;
    }
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    static char s[20];
    int top = 0;
    for (; x; x /= 10)
        s[++top] = x % 10 + '0';
    while (top)
        putchar(s[top--]);
}
// EOT

const int MAXN = 1e5 + 5;
const int MOD = 1e9 + 7;

int N;
int A[MAXN];

void input()
{
    read(N);
    for (int i = 1; i <= N; ++i) {
        read(A[i]);
    }
}

LL countPureCycle(int len, int cnt)
{
    // printf("countPureCycle(%d,%d)\n", len, cnt);
    static LL dp[MAXN];
    int t = (len & 1) && len > 1;
    dp[0] = 1;
    dp[1] = 1 + t;
    for (int i = 2; i <= cnt; ++i) {
        dp[i] = (dp[i - 1] * (1 + t) % MOD + dp[i - 2] * (i - 1) % MOD * len % MOD) % MOD;
    }
    return dp[cnt];
}

void solve()
{
    static bool vis[MAXN];
    static int indeg[MAXN], cnt[MAXN];
    static int cid[MAXN], len[MAXN], len0[MAXN];
    int totc = 0;

    for (int i = 1; i <= N; ++i) {
        ++indeg[A[i]];
    }
    for (int i = 1; i <= N; ++i) {
        if (indeg[i])
            continue;
        int j = i;
        for (; !vis[j]; j = A[j]) {
            vis[j] = true;
        }
        if (!cid[j]) {
            cid[j] = ++totc;
            for (int k = A[j]; k != j; k = A[k]) {
                cid[k] = ++totc;
            }
        }
        if (len[j]) {
            puts("0");
            return;
        }
        for (int k = i; k != j; k = A[k]) {
            ++len[j];
        }
    }
    for (int i = 1; i <= N; ++i) {
        if (vis[i])
            continue;
        int t = 0;
        for (int j = i; !vis[j]; j = A[j]) {
            vis[j] = true;
            ++t;
        }
        ++cnt[t];
    }

    int ans = 1;
    for (int i = 1; i <= N; ++i) {
        if (!len[i])
            continue;
        int t = 1, j = A[i];
        for (; !len[j]; j = A[j]) {
            ++t;
        }
        len0[j] = t;
    }
    for (int i = 1; i <= N; ++i) {
        if (!len[i])
            continue;
        // printf("%d: len=%d, len0=%d\n", i, len[i], len0[i]);
        if (len[i] > len0[i]) {
            puts("0");
            return;
        }
        ans = (LL)ans * (len[i] < len0[i] ? 2 : 1) % MOD;
    }
    for (int i = 1; i <= N; ++i) {
        ans = (LL)ans * countPureCycle(i, cnt[i]) % MOD;
    }

    printf("%d\n", ans);
}

int main()
{
#ifdef __DEBUG
    freopen("E.in", "r", stdin);
    freopen("E.out", "w", stdout);
#endif

    input();
    solve();

    return 0;
}

// 黄沙百战穿金甲，不破楼兰终不还。
//     -- 王昌龄《从军行七首·其四》