CF711D.Directed Roads

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

ZS the Coder and Chris the Baboon has explored Udayland for quite some time. They realize that it consists of nn towns numbered from 11 to nn .

There are nn directed roads in the Udayland. ii -th of them goes from town ii to some other town aia_{i} ( aiia_{i}≠i ). ZS the Coder can flip the direction of any road in Udayland, i.e. if it goes from town AA to town BB before the flip, it will go from town BB to town AA after.

ZS the Coder considers the roads in the Udayland confusing, if there is a sequence of distinct towns A1,A2,...,AkA_{1},A_{2},...,A_{k} ( k>1 ) such that for every 1<=i<k there is a road from town AiA_{i} to town Ai+1A_{i+1} and another road from town AkA_{k} to town A1A_{1} . In other words, the roads are confusing if some of them form a directed cycle of some towns.

Now ZS the Coder wonders how many sets of roads (there are 2n2^{n} variants) in initial configuration can he choose to flip such that after flipping each road in the set exactly once, the resulting network will not be confusing.

Note that it is allowed that after the flipping there are more than one directed road from some town and possibly some towns with no roads leading out of it, or multiple roads between any pair of cities.

输入格式

The first line of the input contains single integer nn ( 2<=n<=21052<=n<=2·10^{5} ) — the number of towns in Udayland.

The next line contains nn integers a1,a2,...,ana_{1},a_{2},...,a_{n} (1<=ai<=n,aii)(1<=a_{i}<=n,a_{i}≠i) , aia_{i} denotes a road going from town ii to town aia_{i} .

输出格式

Print a single integer — the number of ways to flip some set of the roads so that the resulting whole set of all roads is not confusing. Since this number may be too large, print the answer modulo 109+710^{9}+7 .

输入输出样例

  • 输入#1

    3
    2 3 1
    

    输出#1

    6
    
  • 输入#2

    4
    2 1 1 1
    

    输出#2

    8
    
  • 输入#3

    5
    2 4 2 5 3
    

    输出#3

    28
    

说明/提示

Consider the first sample case. There are 33 towns and 33 roads. The towns are numbered from 11 to 33 and the roads are , , initially. Number the roads 11 to 33 in this order.

The sets of roads that ZS the Coder can flip (to make them not confusing) are 1,2,3,1,2,1,3,2,3{1},{2},{3},{1,2},{1,3},{2,3} . Note that the empty set is invalid because if no roads are flipped, then towns 1,2,31,2,3 is form a directed cycle, so it is confusing. Similarly, flipping all roads is confusing too. Thus, there are a total of 66 possible sets ZS the Coder can flip.

The sample image shows all possible ways of orienting the roads from the first sample such that the network is not confusing.

首页