ZS the Coder and Chris the Baboon has explored Udayland for quite some time. They realize that it consists of n towns numbered from1 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.

Input

The first line of the input contains single integer n (2 ≤ n ≤ 2·105) — the number of towns in Udayland.

The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n, ai ≠ i), ai denotes a road going from town i to town ai.

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.

Examples
input
3
2 3 1
output
6
input
4
2 1 1 1
output
8
input
5
2 4 2 5 3
output
28
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.


题目大意

  给定一个有向图,每个点的出度为1。有一种操作可以将一条边的方向翻转,问有多少种不同的操作方案使得操作后图中不存在点数大于1的强连通分量。

  因为图很特殊,所以有比较特殊的计算方法。

  容易发现,对于已经存在的强连通分量,只有全部翻转中间的边和什么都不做的量两种方案不可行,其他都可行。

  对于不在强连通分量内的边,可翻转也可以不翻转。

  然后用dfs找环,用乘法原理乘一乘就好。

Code

 /**
* Codeforces
* Problem#711D
* Accepted
* Time: 62ms
* Memory: 4376k
*/
#include <bits/stdc++.h>
using namespace std; const int M = 1e9 + ; int n;
int cnt = ;
int ric = ;
int *suf;
int *vid;
int *bel; int qpow(int a, int pos) {
int rt = , pa = a;
for (; pos; pos >>= , pa = pa * 1ll * pa % M)
if (pos & )
rt = rt * 1ll * pa % M;
return rt;
} inline void init() {
scanf("%d", &n);
suf = new int[(n + )];
vid = new int[(n + )];
bel = new int[(n + )];
for (int i = ; i <= n; i++)
scanf("%d", suf + i);
memset(vid, , sizeof(int) * (n + ));
memset(bel, , sizeof(int) * (n + ));
} int dfs(int node, int id) {
vid[node] = ++cnt;
bel[node] = id;
int e = suf[node];
if (vid[e])
return (bel[e] == id) ? (vid[node] - vid[e] + ) : ();
return dfs(e, id);
} int ans = ;
inline void solve() {
for (int i = , s; i <= n; i++)
if (!vid[i]) {
s = dfs(i, i);
if(s)
ans = (ans * 1ll * ((qpow(, s) + M - ) % M)) % M;
ric += s;
}
ans = (ans * 1ll * qpow(, n - ric)) % M;
printf("%d\n", ans);
} int main() {
init();
solve();
return ;
}

Codeforces 711D Directed Roads - 组合数学的更多相关文章

  1. codeforces 711D Directed Roads(DFS)

    题目链接:http://codeforces.com/problemset/problem/711/D 思路:由于每个点出度都为1,所以没有复杂的环中带环.DFS遍历,若为环则有2^k-2种,若为链则 ...

  2. 【图论】Codeforces 711D Directed Roads

    题目链接: http://codeforces.com/problemset/problem/711/D 题目大意: 给一张N个点N条有向边的图,边可以逆向.问任意逆向若干条边使得这张图无环的方案数( ...

  3. CodeForces 711D Directed Roads (DFS判环+计数)

    题意:给定一个有向图,然后你可能改变某一些边的方向,然后就形成一种新图,让你求最多有多少种无环图. 析:假设这个图中没有环,那么有多少种呢?也就是说每一边都有两种放法,一共有2^x种,x是边数,那么如 ...

  4. CodeForces 711D Directed Roads

    计数,模拟. 首先观察一下给出的图的特点: $1.$一定存在环. $2.$可能存在多个环. 我们对每个环计算方案数,假设环$C$上包含$x$条边,那么把环$C$破坏掉的方案数有${2^x} - 2$种 ...

  5. CodeForces 711D Directed Roads (DFS找环+组合数)

    <题目链接> 题目大意: 给定一个$n$条边,$n$个点的图,每个点只有一条出边(初始状态),现在能够任意对图上的边进行翻转,问你能够使得该有向图不出先环的方案数有多少种. 解题分析: 很 ...

  6. Code Forces 711D Directed Roads

    D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  7. codeforces 711D D. Directed Roads(dfs)

    题目链接: D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  8. 【34.40%】【codeforces 711D】Directed Roads

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  9. Directed Roads CodeForces - 711D (基环外向树 )

    ZS the Coder and Chris the Baboon has explored Udayland for quite some time. They realize that it co ...

随机推荐

  1. 数据加密之MD5加密

    MD5是一个安全的散列算法,有两个特点:1.输入两个不同的明文(一段原始的数字信息)不会得到相同的输出值2.根据输出值,不能得到原始的明文,即过程不可逆所以要解密MD5没有现成的算法,只能用穷举法,把 ...

  2. java 运行时异常与非运行时异常理解

    参考:https://blog.csdn.net/lan12334321234/article/details/70049446 所谓的异常就是阻止当前程序或方法继续执行的问题 java异常分为两种: ...

  3. 关于随机数、方法重载和System.out.println()的认识

    (1)使用纯随机数发生器编写一个指定数目内数字的程序(类真随机数) 源代码: package Demo1; public class trueRandom { long Multiplier = 45 ...

  4. SSM框架-SpringMVC 实例文件上传下载

    一.新建一个Web工程,导入相关的包 springmvc的包+commons-fileupload.jar+connom-io.jar+commons-logging,jar+jstl.jar+sta ...

  5. Python记录5:函数1

    函数 ''' 1. 什么是函数     在程序中具备某一功能的工具->函数     函数的使用必须遵循原则:         先定义         后调用 函数分为两大类:         1 ...

  6. MD5解密

      国外http://md5.rednoize.com/http://www.milw0rm.com/md5/list.php国内http://www.neeao.com/md5/http://www ...

  7. 【Linux学习五】文本处理

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 一.cut:显示切割的行数据f:选择显示的列s:不显示没有分隔符的行d ...

  8. VIM For Windows 1

    some tips for using vim in windows. 1,download the software vim and install it, you can go to the Of ...

  9. 决策树算法——ID3

    决策树算法是一种有监督的分类学习算法.利用经验数据建立最优分类树,再用分类树预测未知数据. 例子:利用学生上课与作业状态预测考试成绩. 上述例子包含两个可以观测的属性:上课是否认真,作业是否认真,并以 ...

  10. Runtime单例模式类 -- 控制电脑关机

    package demo1; import java.io.IOException; public class RunTimeDemo { public static void main(String ...