ZS the Coder and Chris the Baboon has explored Udayland for quite some time. They realize that it consists of n towns numbered from 1 to n.
There are n directed roads in the Udayland. i-th of them goes from town i to some other town ai (ai≠i). ZS the Coder can flip the direction of any road in Udayland, i.e. if it goes from town A to town B before the flip, it will go from town B to town A after.
ZS the Coder considers the roads in the Udayland confusing, if there is a sequence of distinct towns A1,A2,...,Ak (k>1) such that for every 1≤i<k there is a road from town Ai to town Ai+1 and another road from town Ak to town A1. 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 2n 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.
Output
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+7.
Note
Consider the first sample case. There are
3 towns and
3 roads. The towns are numbered from
1 to
3 and the roads are
,
,
initially. Number the roads
1 to
3 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}. Note that the empty set is invalid because if no roads are flipped, then towns
1,2,3 is form a directed cycle, so it is confusing. Similarly, flipping all roads is confusing too. Thus, there are a total of
6 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.
编码员ZS和狒狒克里斯已经在乌代兰探索了一段时间。他们意识到它由n个城镇组成,编号从1到n。
乌代兰有n条定向道路。他们中的第i个从城镇i到另一个城镇ai(ai≠i)。ZS编码器可以翻转乌代兰任何道路的方向,即,如果在翻转之前从A镇到B镇,则在翻转之后从B镇到A镇。
ZS编码员认为,如果有一系列不同的城镇A1、A2、,。。。,Ak(k>1),使得每1≤i<k有一条从城镇Ai到城镇Ai+1的道路,以及从城镇Ak到城镇A1的另一条道路。换言之,如果其中一些道路形成了一些城镇的定向循环,那么这些道路就会令人困惑。
现在,编码者ZS想知道,在初始配置中,他可以选择翻转多少组道路(有2n个变体),以便在翻转组中的每条道路一次之后,生成的网络不会令人困惑。
请注意,翻转后,允许从某个城镇(可能是一些没有道路的城镇)或任何一对城市之间存在多条道路。