Pinely Round 2 (Div. 1 + Div. 2)
G
Swaps
Codeforces
2800
原题
← FH →
翻译中…

You are given an array of integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$). You can perform the following operation several (possibly, zero) times:

翻译中…
  • pick an arbitrary $i$ and perform swap$(a_i, a_{a_i})$.
翻译中…

How many distinct arrays is it possible to attain? Output the answer modulo $(10^9 + 7)$.

翻译中…

Input

翻译中…

The first line contains an integer $n$ ($1 \le n \le 10^6$).

翻译中…

The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1\le a_i\le n$).

翻译中…

Output

翻译中…

Output the number of attainable arrays modulo $(10^9 + 7)$.

翻译中…

Note

翻译中…

In the first example, the initial array is $[1, 1, 2]$. If we perform the operation with $i = 3$, we swap $a_3$ and $a_2$, obtaining $[1, 2, 1]$. One can show that there are no other attainable arrays.

翻译中…

In the second example, the four attainable arrays are $[2, 1, 4, 3]$, $[1, 2, 4, 3]$, $[1, 2, 3, 4]$, $[2, 1, 3, 4]$. One can show that there are no other attainable arrays.

翻译中…

样例

Input #1
3
1 1 2
Output #1
2
InputCopy OutputCopy
Input #2
4
2 1 4 3
Output #2
4
InputCopy OutputCopy
Input #3
6
2 3 1 1 1 2
Output #3
18
InputCopy OutputCopy
题目信息
题号
1863G
时间限制
2 s
内存限制
256 MB
标签
组合数学
动态规划
图论
数学

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。