###### codeforces Round#369 div2-D tDirected Roads

1.形成环的强连通：2^n-2（本身和所有边反向不合法，其余都合法）;

2.单点的强连通：2（该点的出边对合法性无影响）；

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>

using namespace std;

typedef long long LL;
const int M=1000000007;
const int N=400000+10;

vector<int>V[N];
int n;
int Time,dfn[N],low[N],sta[N],instack[N],g[N];
int belong[N],h,Col;
LL RP(LL a,LL b)
{
LL Ans=1;
for (;b;b>>=1){
if (b&1)Ans=Ans*a%M;
a=a*a%M;
}
return Ans;
}
void tarjan(int u)
{
Time++;
dfn[u]=low[u]=Time;
sta[++h]=u;instack[u]=1;
for (int i=0;i<V[u].size();i++){
int v=V[u][i];
if (!dfn[v]){
tarjan(v);
low[u]=min(low[u],low[v]);
}else if (instack[v]==1)low[u]=min(low[u],low[v]);
}
if (dfn[u]==low[u]){
Col++;
while (sta[h]!=u){instack[sta[h]]=-1;belong[sta[h]]=Col;h--;g[Col]++;}
instack[sta[h]]=-1;belong[sta[h]]=Col;h--;g[Col]++;
}
}
int main()
{
//freopen("1.txt","r",stdin);
scanf("%d",&n);
for (int i=1;i<=n;i++){
int a;scanf("%d",&a);
V[i].push_back(a);
}
memset(g,0,sizeof g);
memset(dfn,0,sizeof dfn);
memset(low,0,sizeof low);
memset(instack,0,sizeof instack);
h=Time=Col=0;
for (int i=1;i<=n;i++)if (!dfn[i])tarjan(i);
LL Ans=1;
for (int i=1;i<=Col;i++)if (g[i]>1){
Ans=Ans*(RP(2,g[i])-2+M)%M;
}else Ans=Ans*2%M;
cout<<Ans<<endl;
return 0;
}


#### Codeforces 543B Destroying Roads 【暴力 SPFA】

2015-11-10 20:40:13

#### [最短路 杂题] Codeforces 806D VK Cup 2017 Round 3 D. Perishable Roads

2017-05-20 07:20:54

#### 你懂C语言，我不信（C深度提高）

2016-08-30 15:01:42

#### Codeforces 543B. Destroying Roads 最短路+暴力

2015-05-14 09:15:25

#### 最短路+tarjan codeforces567E President and Roads

2015-08-08 17:00:50

#### CodeForces 567E President and Roads（最短路 + tarjan）

2015-08-07 16:45:29

#### Codeforces 240E. Road Repairs 最小树形图+输出路径

2015-07-08 17:45:32

#### CodeForces 25C Roads in Berland【floyd过】

2016-02-17 15:21:07

#### 从新开始，codeforces 567e

2015-08-06 21:36:07

#### Codeforces 191C Fools and Roads(树链剖分)

2014-10-17 18:47:22