#AGC008E. [AGC008E] Next or Nextnext

[AGC008E] Next or Nextnext

Score : 14001400 points

Problem Statement

You are given an integer sequence aa of length NN. How many permutations pp of the integers 11 through NN satisfy the following condition?

  • For each 1iN1 \leq i \leq N, at least one of the following holds: pi=aip_i = a_i and ppi=aip_{p_i} = a_i.

Find the count modulo 109+710^9 + 7.

Constraints

  • 1N1051 \leq N \leq 10^5
  • aia_i is an integer.
  • 1aiN1 \leq a_i \leq N

Input

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

NN

a1a_1 a2a_2 ...... aNa_N

Output

Print the number of the permutations pp that satisfy the condition, modulo 109+710^9 + 7.

3
1 2 3
4

The following four permutations satisfy the condition:

  • (1,2,3)(1, 2, 3)
  • (1,3,2)(1, 3, 2)
  • (3,2,1)(3, 2, 1)
  • (2,1,3)(2, 1, 3)

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

2
1 1
1

The following one permutation satisfies the condition:

  • (2,1)(2, 1)
3
2 1 1
2

The following two permutations satisfy the condition:

  • (2,3,1)(2, 3, 1)
  • (3,1,2)(3, 1, 2)
3
1 1 1
0
13
2 1 4 3 6 7 5 9 10 8 8 9 11
6