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. 获取 Google USB 驱动程序

    获取 Google USB 驱动程序       另请参阅 安装 USB 驱动程序 使用硬件设备 使用任何 Google Nexus 设备进行 ADB 调试时,只有 Windows 需要 Google ...

  2. Pycharm进行版本管理

    即然pycharm为python提供了这么强大的IDE,那么,我们代码管理,没理由不用版本管理工具Git,SVN等等 在pychram中使用GitHub进行代码管理;需要准备: 1)GitHub帐号: ...

  3. ORA-01919: role 'PLUSTRACE' does not exist

    环境:Oracle 10g,11g. 现象:在一次迁移测试中,发现有这样的角色赋权会报错不存在: SYS@orcl> grant PLUSTRACE to jingyu; grant PLUST ...

  4. js语法没有任何问题但是就是不走,检查js中命名的变量名,用 service-area错误,改service_area (原)

    js语法没有任何问题但是就是不走,检查js中命名的变量名,用 service-area错误,改service_area

  5. ModelSim仿真教程

    本文章详细介绍了怎样用ModelSim仿真Verilog,虽然只是很简单的一个二分频器的例子,但却正式小白入门所需要的. 本教程以ModelSim SE 10.4为例 1. 新建工程 file-> ...

  6. 谈谈html中一些比较"偏门"的知识(map&area;iframe;label)

    说明:这里所说的"偏门"只是相对于本人而言,记录在此,加深印象.也希望有需要的朋友能获得些许收获! 1.空元素(void):没有内容的元素. 常见的有:<br>,< ...

  7. 一 django框架?

    Django-1   一 什么是web框架? 框架,即framework,特指为解决一个开放性问题而设计的具有一定约束性的支撑结构,使用框架可以帮你快速开发特定的系统,简单地说,就是你用别人搭建好的舞 ...

  8. C/C++笔试题(编程题)

    面试过程中遇到的编程题整理,于此备录.分享,共勉.(持续更新中......欢迎补充) (1)用户输入M, N值,从1至N开始顺序循环数数,每数到M输出该数值,直至全部输出.写出C程序. 程序代码如下: ...

  9. redis的优缺点和使用场景

    1. 使用redis有哪些好处? (1) 速度快,因为数据存在内存中,类似于HashMap,HashMap的优势就是查找和操作的时间复杂度都是O(1) (2) 支持丰富数据类型,支持string,li ...

  10. 设计模式之Decorator(油漆工)(转)

    Decorator常被翻译成"装饰",我觉得翻译成"油漆工"更形象点,油漆工(decorator)是用来刷油漆的,那么被刷油漆的对象我们称decoratee.这 ...