# Codeforces Round #369 (Div. 2) D. Directed Roads

——————因为这里每个点的出度只有1，所有要成环肯定是沿着给定nxt找下去找得到。

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define mod 1000000007
const int maxn=200005;
int nxt[maxn],vis[maxn],loop[maxn];
int n,tot;
void findloop(int x){
loop[tot]=1;
vis[x]=3;
while(vis[nxt[x]]!=3){
x=nxt[x];
vis[x]=3;
loop[tot]++;
}
}
void dfs(int x){
vis[x]=2;
if(!vis[nxt[x]])dfs(nxt[x]);
if(vis[nxt[x]]==2){
tot++;
findloop(nxt[x]);
}
//if(vis[nxt[x]]==1||vis[nxt[x]]==3)
vis[x]=1;
}
LL quickpowmod(LL x,LL y,LL mo)
{
LL ret = 1;
while(y){
if(y&1)
ret = ret*x%mo;
x = x*x%mo;
y >>= 1;
}
return ret;
}

int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&nxt[i]);
tot=0;
for(int i=1;i<=n;i++){
if(!vis[i])dfs(i);
}
LL ans=1;
for(int i=1;i<=tot;i++){
ans=ans*((quickpowmod(2,loop[i],mod)-2+mod)%mod)%mod;
n-=loop[i];
}
ans=ans*quickpowmod(2,n,mod)%mod;
cout<<ans<<endl;
return 0;
}


#### Codeforces Round #444 (Div. 2)-贪心&尺取-Ratings and Reality Shows

2017-11-11 20:11:11

#### Codeforces Round #446 (Div. 2) D. Gluttony

2017-11-18 21:07:25

#### CF-Codeforces Round #483 (Div. 2)-D-XOR-pyramid

2018-05-16 01:15:04

#### Codeforces Round #426 (Div. 2) D. The Bakery（DP+线段树）

2017-07-31 09:16:44

#### Educational Codeforces Round 36 (Rated for Div. 2) 题解

2018-01-13 23:16:24

#### Codeforces Round #476 (Div. 2) [Thanks, Telegram!] D - Single-use Stones（思维）

2018-04-27 00:50:37

#### Codeforces Round #480 (Div. 2): D. Perfect Groups（思维题）

2018-05-09 17:29:44

#### Codeforces Round #483 (Div. 2) D. XOR-pyramid

2018-05-16 14:27:28

#### Codeforces Round #402 (Div. 2)C. Dishonest Sellers【贪心】这个贪心好水啊....

2017-02-27 13:07:44

#### CF-Codeforces Round #485 (Div. 2)-E-Petr and Permutations

2018-05-30 17:33:35