#agc008e. [agc008_e]Next or Nextnext

[agc008_e]Next or Nextnext

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 ≤ i ≤ 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 ≤ N ≤ 10^5
  • aia_i is an integer.
  • 1aiN1 ≤ a_i ≤ 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.


Sample Input 1

3
1 2 3

Sample Output 1

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.


Sample Input 2

2
1 1

Sample Output 2

1

The following one permutation satisfies the condition:

  • (2,1)(2, 1)

Sample Input 3

3
2 1 1

Sample Output 3

2

The following two permutations satisfy the condition:

  • (2,3,1)(2, 3, 1)
  • (3,1,2)(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