CF1863G.Swaps

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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

  • pick an arbitrary ii and perform swap (ai,aai)(a_i, a_{a_i}) .

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

输入格式

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

The second line contains nn integers a1,a2,,ana_1, a_2, \dots, a_n ( 1ain1\le a_i\le n ).

输出格式

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

输入输出样例

  • 输入#1

    3
    1 1 2

    输出#1

    2
  • 输入#2

    4
    2 1 4 3

    输出#2

    4
  • 输入#3

    6
    2 3 1 1 1 2

    输出#3

    18

说明/提示

In the first example, the initial array is [1,1,2][1, 1, 2] . If we perform the operation with i=3i = 3 , we swap a3a_3 and a2a_2 , obtaining [1,2,1][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][2, 1, 4, 3] , [1,2,4,3][1, 2, 4, 3] , [1,2,3,4][1, 2, 3, 4] , [2,1,3,4][2, 1, 3, 4] . One can show that there are no other attainable arrays.

首页