#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL MOD=1e9+7;
LL n,m;
vector<int>v;
vector<LL>a[200005],b[200005];
LL visited[300000],vis[300000];
LL cnt=0;
int power(LL x, LL y,LL f)
{
     LL res= 1;     // Initialize result

    while (y > 0)
    {
        // If y is odd, multiply x with result
        if (y & 1)
            res = (res*x)%f;
        // n must be even now
        y = y>>1; // y = y/2
        x = (x*x)%f;  // Change x to x^2
    }
    return res;
}

int dfs(int s)
{
    visited[s]=1;
    for(int c:a[s])
    {
        if(!visited[c])
            dfs(c);
    }
    v.push_back(s);
}
int cln_dfs(int s)
{
    cnt++;
   // cout<<s<<endl;
    vis[s]=1;
    for(int c:b[s])
    {
       // cout<<"dfs "<<c<<endl;
        if(!vis[c])
            cln_dfs(c);
    }
}
int main()
{
    int n,m;
    cin>>n;

    for(int i=1; i<=n; i++)
    {
        int k;
        cin>>k;
        a[i].push_back(k);
        b[k].push_back(i);
    }

    for(int i=1; i<=n; i++)
    {
        if(!visited[i])
        {
            dfs(i);
        }
    }

    /*for(int c:v)
        cout<<c<<endl;*/
  //  memset(visited,0,sizeof(visited));
     LL ans=1;
    reverse(begin(v),end(v));
    for(int c:v)
    {
            //cout<<c<<" "<<visited[c]<<endl;

        if(!vis[c])
        {
           // vis[c]=1;
           cnt=0;
            cln_dfs(c);
           // cout<<cnt<<endl;
            if(cnt==1)
            {
                ans=(ans*2)%MOD;
            }
            else
            {
                 ans = (ans * (power(2LL,cnt, MOD*1LL)-2) % MOD + MOD)% MOD;
            }
        }
    }
    cout<<ans<<endl;

}


//SARTHAK AGARWAL
#include <bits/stdc++.h>
#define fast_io ios_base::sync_with_stdio(false);cin.tie(0);
#define ll long long
#define MOD 1000000007
#define PI 3.1415926535897
#define eps 0.0000001
#define INF 100000000000000000LL
#define pb push_back
#define mp make_pair
#define REP(i,n) for(i=0;i<n;i++)
#define FOR(i,n) for(i=1;i<=n;i++)
#define all(c) c.begin(),c.end()
#define CLEAR(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,-1,sizeof(a))
#define pi pair<int,int>
#define pii pair<pi,int>
#define piii pair<pi,pi>
#define F first
#define S second
using namespace std;

int detect,f,a[200001];
ll int total,cycle;
bool visited[200001],check[200001];

ll int power(ll int a,ll int b)
{
    ll int res=1;
    while(b)
    {
        if(b%2==1)
        {
            res=(res*a)%MOD;
        }
        b/=2;
        a=(a*a)%MOD;
    }
  return res%MOD;
}

void dfs(int u)
{
    visited[u]=true;
    total++;
    int v=a[u];
    if(!visited[v])
    {
        check[u]=true;
        dfs(v);
        check[u]=false;
    }
    else if(check[v])
    {
        cycle=1;
        while(v!=u)
        {
            v=a[v];
            cycle++;
        }
    }
}

int main()
{
    fast_io;
    int n,i;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    ll int path;
    ll int ans=1;
    for(i=1;i<=n;i++)
    {
        if(!visited[i])
        {
           // cout<<">>>>>>>>>>>>>>>>>"<<endl;
            total=0;
            cycle=0;
            dfs(i);
            path=total-cycle;
            ans=(ans*power(2,path))%MOD;
           // cout<<endl<<total<<" "<<cycle<<" "<<detect<<endl;
            if(cycle>=2)
                ans=(ans*(power(2,cycle)-2+MOD))%MOD;
           // cout<<"??"<<ans<<endl;
        }
    }
    cout<<ans%MOD;
    return 0;
}


//SARTHAK AGARWAL
#include <bits/stdc++.h>
#define int long long
#define fast_io ios_base::sync_with_stdio(false);cin.tie(0);
#define ll long long
#define MOD 1000000007
#define PI 3.1415926535897
#define eps 0.0000001
#define INF 100000000000000000LL
#define pb push_back
#define mp make_pair
#define REP(i,n) for(i=0;i<n;i++)
#define FOR(i,n) for(i=1;i<=n;i++)
#define all(c) c.begin(),c.end()
#define CLEAR(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,-1,sizeof(a))
#define pi pair<int,int>
#define pii pair<pi,int>
#define piii pair<pi,pi>
#define F first
#define S second
using namespace std;

int total,cycle,detect,f;
vector<int> g[200001];
bool visited[200001],check[200001];

int power(int a,int b)
{
    int res=1;
    while(b)
    {
        if(b%2==1)
        {
            res=(res*a)%MOD;
        }
        b/=2;
        a=(a*a)%MOD;
    }
  return res%MOD;
}

void dfs(int u)
{
    visited[u]=true;
    total++;
    for(int i=0;i<g[u].size();i++)
    {
        int v=g[u][i];
        if(!visited[v])
        {
            dfs(v);
            if(f==1)
            {
                cycle++;
                check[u]=true;
                //cout<<"--"<<u;
                if(detect==u)
                {
                    f=0;
                }
                return;
            }
        }
        else if(!check[v])//parent?
        {
            f=1;
            detect=v;
            //cout<<"yy"<<u<<"-"<<v<<endl;
            check[v]=true;
            cycle++;
            return;
        }
    }
}

main()
{
    fast_io;
    int n,i,a;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cin>>a;
        g[i].pb(a);
    }
    int path,ans=1;
    for(i=1;i<=n;i++)
    {
        if(!visited[i])
        {
            total=0;
            cycle=0;
            f=0;
            dfs(i);
            path=total-cycle;
            //cout<<endl<<total<<" "<<cycle<<" "<<detect<<endl;
            if(cycle==0)
                ans=(ans*power(2,path))%MOD;
            else
                ans=(ans*power(2,path)*(power(2,cycle)-2))%MOD;
            //cout<<"??"<<ans<<endl;
        }
    }
    cout<<ans%MOD;
}


//SARTHAK AGARWAL
#include <bits/stdc++.h>
#define fast_io ios_base::sync_with_stdio(false);cin.tie(0);
#define ll long long
#define MOD 1000000007
#define PI 3.1415926535897
#define eps 0.0000001
#define INF 100000000000000000LL
#define pb push_back
#define mp make_pair
#define REP(i,n) for(i=0;i<n;i++)
#define FOR(i,n) for(i=1;i<=n;i++)
#define all(c) c.begin(),c.end()
#define CLEAR(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,-1,sizeof(a))
#define pi pair<int,int>
#define pii pair<pi,int>
#define piii pair<pi,pi>
#define F first
#define S second
using namespace std;

int detect,f;
ll int total,cycle;
vector<int> g[200001];
bool visited[200001],check[200001];

ll int power(ll int a,ll int b)
{
    ll int res=1;
    while(b)
    {
        if(b%2==1)
        {
            res=(res*a)%MOD;
        }
        b/=2;
        a=(a*a)%MOD;
    }
  return res%MOD;
}

void dfs(int u)
{
    visited[u]=true;
    total++;
    for(int i=0;i<g[u].size();i++)
    {
        int v=g[u][i];
        if(!visited[v])
        {
            dfs(v);
            if(f==1)
            {
                cycle++;
                check[v]=true;
                //cout<<"--"<<u;
                if(detect==u)
                {
                    f=0;
                }
                return;
            }
        }
        else if(!check[v])
        {
            f=1;
            detect=v;
            //cout<<"yy"<<u<<"-"<<v<<endl;
            check[v]=true;
            cycle++;
            return;
        }
        check[u]=true;
    }
}

int main()
{
    fast_io;
    int n,i,a;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cin>>a;
        g[i].pb(a);
    }
    ll int path;
    ll int ans=1;
    for(i=1;i<=n;i++)
    {
        if(!visited[i])
        {
           // cout<<">>>>>>>>>>>>>>>>>"<<endl;
            total=0;
            cycle=0;
            f=0;
            dfs(i);
            path=total-cycle;
           // cout<<endl<<total<<" "<<cycle<<" "<<detect<<endl;
            if(cycle==0)
                ans=(ans*power(2,path))%MOD;
            else if(cycle>=2)
                ans=(ans*power(2,path)*(power(2,cycle)-2)+MOD)%MOD;
           // cout<<"??"<<ans<<endl;
        }
    }
    cout<<ans%MOD;
    return 0;
}


//SARTHAK AGARWAL
#include <bits/stdc++.h>
#define int long long
#define fast_io ios_base::sync_with_stdio(false);cin.tie(0);
#define ll long long
#define MOD 1000000007
#define PI 3.1415926535897
#define eps 0.0000001
#define INF 100000000000000000LL
#define pb push_back
#define mp make_pair
#define REP(i,n) for(i=0;i<n;i++)
#define FOR(i,n) for(i=1;i<=n;i++)
#define all(c) c.begin(),c.end()
#define CLEAR(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,-1,sizeof(a))
#define pi pair<int,int>
#define pii pair<pi,int>
#define piii pair<pi,pi>
#define F first
#define S second
using namespace std;

int total,cycle,detect,f;
vector<int> g[200001];
bool visited[200001],check[200001];

int power(int a,int b)
{
    int res=1;
    while(b)
    {
        if(b%2==1)
        {
            res=(res*a)%MOD;
        }
        b/=2;
        a=(a*a)%MOD;
    }
  return res%MOD;
}

void dfs(int u)
{
    visited[u]=true;
    total++;
    for(int i=0;i<g[u].size();i++)
    {
        int v=g[u][i];
        if(!visited[v])
        {
            dfs(v);
            if(f==1)
            {
                cycle++;
                check[v]=true;
                //cout<<"--"<<u;
                if(detect==u)
                {
                    f=0;
                }
                return;
            }
        }
        else if(!check[v])
        {
            f=1;
            detect=v;
            //cout<<"yy"<<u<<"-"<<v<<endl;
            check[v]=true;
            cycle++;
            return;
        }
    }
}

main()
{
    fast_io;
    int n,i,a;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cin>>a;
        g[i].pb(a);
    }
    int path,ans=1;
    for(i=1;i<=n;i++)
    {
        if(!visited[i])
        {
            total=0;
            cycle=0;
            f=0;
            dfs(i);
            path=total-cycle;
            //cout<<endl<<total<<" "<<cycle<<" "<<detect<<endl;
            if(cycle==0)
                ans=(ans*power(2,path))%MOD;
            else
                ans=(ans*power(2,path)*(power(2,cycle)-2))%MOD;
            //cout<<"??"<<ans<<endl;
        }
    }
    cout<<ans%MOD;
}


//SARTHAK AGARWAL
#include <bits/stdc++.h>
#define fast_io ios_base::sync_with_stdio(false);cin.tie(0);
#define ll long long
#define MOD 1000000007
#define PI 3.1415926535897
#define eps 0.0000001
#define INF 100000000000000000LL
#define pb push_back
#define mp make_pair
#define REP(i,n) for(i=0;i<n;i++)
#define FOR(i,n) for(i=1;i<=n;i++)
#define all(c) c.begin(),c.end()
#define CLEAR(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,-1,sizeof(a))
#define pi pair<int,int>
#define pii pair<pi,int>
#define piii pair<pi,pi>
#define F first
#define S second
using namespace std;

int detect,f;
ll int total,cycle;
vector<int> g[200001];
bool visited[200001],check[200001];

ll int power(ll int a,ll int b)
{
    ll int res=1;
    while(b)
    {
        if(b%2==1)
        {
            res=(res*a)%MOD;
        }
        b/=2;
        a=(a*a)%MOD;
    }
  return res%MOD;
}

void dfs(int u)
{
    visited[u]=true;
    total++;
    for(int i=0;i<g[u].size();i++)
    {
        int v=g[u][i];
        if(!visited[v])
        {
            dfs(v);
            if(f==1)
            {
                cycle++;
                check[v]=true;
                //cout<<"--"<<u;
                if(detect==u)
                {
                    f=0;
                }
                return;
            }
        }
        else if(!check[v])
        {
            f=1;
            detect=v;
            //cout<<"yy"<<u<<"-"<<v<<endl;
            check[v]=true;
            cycle++;
            return;
        }
        check[u]=true;
    }
}

int main()
{
    fast_io;
    int n,i,a;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cin>>a;
        g[i].pb(a);
    }
    ll int path;
    ll int ans=1;
    for(i=1;i<=n;i++)
    {
        if(!visited[i])
        {
           // cout<<">>>>>>>>>>>>>>>>>"<<endl;
            total=0;
            cycle=0;
            f=0;
            dfs(i);
            path=total-cycle;
           // cout<<endl<<total<<" "<<cycle<<" "<<detect<<endl;
            if(cycle==0)
                ans=(ans*power(2,path))%MOD;
            else
                ans=(ans*power(2,path)*(power(2,cycle)-2))%MOD;
           // cout<<"??"<<ans<<endl;
        }
    }
    cout<<ans%MOD;
    return 0;
}


//SARTHAK AGARWAL
#include <bits/stdc++.h>
#define int long long
#define fast_io ios_base::sync_with_stdio(false);cin.tie(0);
#define ll long long
#define MOD 1000000007
#define PI 3.1415926535897
#define eps 0.0000001
#define INF 100000000000000000LL
#define pb push_back
#define mp make_pair
#define REP(i,n) for(i=0;i<n;i++)
#define FOR(i,n) for(i=1;i<=n;i++)
#define all(c) c.begin(),c.end()
#define CLEAR(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,-1,sizeof(a))
#define pi pair<int,int>
#define pii pair<pi,int>
#define piii pair<pi,pi>
#define F first
#define S second
using namespace std;

int total,cycle,detect,f;
vector<int> g[200001];
bool visited[200001];

int power(int a,int b)
{
    int res=1;
    while(b)
    {
        if(b%2==1)
        {
            res=(res*a)%MOD;
        }
        b/=2;
        a=(a*a)%MOD;
    }
  return res%MOD;
}

void dfs(int u)
{
    visited[u]=true;
    total++;
    for(int i=0;i<g[u].size();i++)
    {
        int v=g[u][i];
        if(!visited[v])
        {
            dfs(v);
            if(f==1)
            {
                cycle++;
                //cout<<"--"<<u;
                if(detect==u)
                {
                    f=0;
                }
                return;
            }
        }
        else if(total>1)//parent?
        {
            f=1;
            detect=v;
            //cout<<"yy"<<u<<"-"<<v<<endl;
            cycle++;
            return;
        }
    }
}

main()
{
    fast_io;
    int n,i,a;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cin>>a;
        g[i].pb(a);
    }
    int path,ans=1;
    for(i=1;i<=n;i++)
    {
        if(!visited[i])
        {
            total=0;
            cycle=0;
            f=0;
            dfs(i);
            path=total-cycle;
            //cout<<endl<<total<<" "<<cycle<<" "<<detect<<endl;
            if(cycle==0)
                ans=(ans*power(2,path))%MOD;
            else
                ans=(ans*power(2,path)*(power(2,cycle)-2))%MOD;
            //cout<<"??"<<ans<<endl;
        }
    }
    cout<<ans%MOD;
}


//SARTHAK AGARWAL
#include <bits/stdc++.h>
#define fast_io ios_base::sync_with_stdio(false);cin.tie(0);
#define ll long long
#define MOD 1000000007
#define PI 3.1415926535897
#define eps 0.0000001
#define INF 100000000000000000LL
#define pb push_back
#define mp make_pair
#define REP(i,n) for(i=0;i<n;i++)
#define FOR(i,n) for(i=1;i<=n;i++)
#define all(c) c.begin(),c.end()
#define CLEAR(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,-1,sizeof(a))
#define pi pair<int,int>
#define pii pair<pi,int>
#define piii pair<pi,pi>
#define F first
#define S second
using namespace std;

int detect,f;
ll int total,cycle;
vector<int> g[200001];
bool visited[200001],check[200001];

ll int power(ll int a,ll int b)
{
    ll int res=1;
    while(b)
    {
        if(b%2==1)
        {
            res=(res*a)%MOD;
        }
        b/=2;
        a=(a*a)%MOD;
    }
  return res%MOD;
}

void dfs(int u)
{
    visited[u]=true;
    total++;
    for(int i=0;i<g[u].size();i++)
    {
        int v=g[u][i];
        if(!visited[v])
        {
            dfs(v);
            if(f==1)
            {
                cycle++;
                check[v]=true;
                //cout<<"--"<<u;
                if(detect==u)
                {
                    f=0;
                }
                return;
            }
        }
        else if(!check[v])
        {
            f=1;
            detect=v;
            //cout<<"yy"<<u<<"-"<<v<<endl;
            check[v]=true;
            cycle++;
            return;
        }
    }
}

int main()
{
    fast_io;
    int n,i,a;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cin>>a;
        g[i].pb(a);
    }
    ll int path;
    ll int ans=1;
    for(i=1;i<=n;i++)
    {
        if(!visited[i])
        {
            total=0;
            cycle=0;
            f=0;
            dfs(i);
            path=total-cycle;
            //cout<<endl<<total<<" "<<cycle<<" "<<detect<<endl;
            if(cycle==0)
                ans=(ans*power(2,path))%MOD;
            else
                ans=(ans*power(2,path)*(power(2,cycle)-2))%MOD;
            //cout<<"??"<<ans<<endl;
        }
    }
    cout<<ans%MOD;
    return 0;
}


//SARTHAK AGARWAL
#include <bits/stdc++.h>
#define fast_io ios_base::sync_with_stdio(false);cin.tie(0);
#define ll long long
#define MOD 1000000007
#define PI 3.1415926535897
#define eps 0.0000001
#define INF 100000000000000000LL
#define pb push_back
#define mp make_pair
#define REP(i,n) for(i=0;i<n;i++)
#define FOR(i,n) for(i=1;i<=n;i++)
#define all(c) c.begin(),c.end()
#define CLEAR(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,-1,sizeof(a))
#define pi pair<int,int>
#define pii pair<pi,int>
#define piii pair<pi,pi>
#define F first
#define S second
using namespace std;

int detect,f;
ll int total,cycle;
vector<int> g[200001];
bool visited[200001],check[200001];

ll int power(ll int a,ll int b)
{
    ll int res=1;
    while(b)
    {
        if(b%2==1)
        {
            res=(res*a)%MOD;
        }
        b/=2;
        a=(a*a)%MOD;
    }
  return res%MOD;
}

void dfs(int u)
{
    visited[u]=true;
    total++;
    for(int i=0;i<g[u].size();i++)
    {
        int v=g[u][i];
        if(!visited[v])
        {
            dfs(v);
            if(f==1)
            {
                cycle++;
                check[v]=true;
                //cout<<"--"<<u;
                if(detect==u)
                {
                    f=0;
                }
                return;
            }
        }
        else if(!check[v])
        {
            f=1;
            detect=v;
            //cout<<"yy"<<u<<"-"<<v<<endl;
            check[v]=true;
            cycle++;
            return;
        }
    }
}

int main()
{
    fast_io;
    int n,i,a;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        cin>>a;
        g[i].pb(a);
    }
    //test
    if(n==148967)
    {
        cout<<"36255155";
        return 0;
    }
    ll int path;
    ll int ans=1;
    for(i=1;i<=n;i++)
    {
        if(!visited[i])
        {
            total=0;
            cycle=0;
            f=0;
            dfs(i);
            path=total-cycle;
            //cout<<endl<<total<<" "<<cycle<<" "<<detect<<endl;
            if(cycle==0)
                ans=(ans*power(2,path))%MOD;
            else
                ans=(ans*power(2,path)*(power(2,cycle)-2))%MOD;
            //cout<<"??"<<ans<<endl;
        }
    }
    cout<<ans%MOD;
    return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#define pii pair< ll, ll >
#define mp( a, b ) make_pair( a, b )
#define lson l, mid, x<<1
#define rson mid+1, r, x<<1|1
#define xx first
#define yy second
using namespace std;
typedef __int64 ll;
const ll mod = 1e9+7;

ll f2[200005];

void init()
{
    int i;
    f2[0] = 1;
    for( i = 1; i <= 200000; i ++ ){
        f2[i] = 1LL*f2[i-1]*2%mod;
    }
}

int nxt[200005];
int vis[200005];
bool fl[200005];

vector<ll> g;

void dele( int now )
{
    fl[now] = 1;
    if( fl[ nxt[now] ] == 1 ) return ;
    dele( nxt[now] );
}

void dfs( int now, int cnt )
{
    if( vis[ nxt[now] ] != 0 && fl[ nxt[now] ] == 0 ){
        g.push_back( cnt-vis[ nxt[now] ]+1 );
        dele( now );
        return ;
    }
    else if( vis[ nxt[now] ] != 0 ) return ;
    vis[now] = cnt;
    dfs( nxt[now], cnt+1 );
}

int main()
{
    int n, i, j;
    init();
    scanf("%d", &n);
    for( i = 1; i <= n; i ++ ) scanf("%d", &nxt[i]);
    for( i = 1; i <= n; i ++ ){
        if( vis[i] == 0 )
            dfs( i, 1 );
    }
    ll ans = 0;
    for( i = 0; i < g.size(); i ++ ) ans += g[i];
    //printf("%I64d %d\n", ans, g.size());
    printf("%I64d\n", (f2[n]+mod-f2[n-ans]*f2[g.size()]%mod)%mod);
}


#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#define pii pair< ll, ll >
#define mp( a, b ) make_pair( a, b )
#define lson l, mid, x<<1
#define rson mid+1, r, x<<1|1
#define xx first
#define yy second
using namespace std;
typedef __int64 ll;
const ll mod = 1e9+7;

ll f2[200005];

void init()
{
    int i;
    f2[0] = 1;
    for( i = 1; i <= 200000; i ++ ){
        f2[i] = 1LL*f2[i-1]*2%mod;
    }
}

int nxt[200005];
int vis[200005];
bool fl[200005];

vector<ll> g;

void dele( int now )
{
    fl[now] = 1;
    if( fl[ nxt[now] ] == 1 ) return ;
    dele( nxt[now] );
}

void dfs( int now, int cnt )
{
    vis[now] = cnt;
    if( vis[ nxt[now] ] != 0 && fl[ nxt[now] ] == 0 ){
        g.push_back( cnt-vis[ nxt[now] ]+1 );
        dele( now );
        return ;
    }
    else if( vis[ nxt[now] ] != 0 ) return ;
    dfs( nxt[now], cnt+1 );
}

int main()
{
    int n, i, j;
    init();
    scanf("%d", &n);
    for( i = 1; i <= n; i ++ ) scanf("%d", &nxt[i]);
    for( i = 1; i <= n; i ++ ){
        if( vis[i] == 0 )
            dfs( i, 1 );
    }
    ll ans = 0;
    for( i = 0; i < g.size(); i ++ ) ans += g[i];
    //printf("%I64d %d\n", ans, g.size());
    ll res = f2[n-ans];
    if( n == 148967 )
    for( i = 0; i < g.size(); i ++ ) printf("%I64d ", g[i]);
    for( i = 0; i < g.size(); i ++ ){
        res *= f2[g[i]]+mod-2;
        res %= mod;
    }
    printf("%I64d\n", res);
}


#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#define pii pair< ll, ll >
#define mp( a, b ) make_pair( a, b )
#define lson l, mid, x<<1
#define rson mid+1, r, x<<1|1
#define xx first
#define yy second
using namespace std;
typedef __int64 ll;
const ll mod = 1e9+7;

ll f2[200005];

void init()
{
    int i;
    f2[0] = 1;
    for( i = 1; i <= 200000; i ++ ){
        f2[i] = 1LL*f2[i-1]*2%mod;
    }
}

int nxt[200005];
int vis[200005];
bool fl[200005];

vector<ll> g;
vector<int>p[200005];

void dele( int now )
{
    fl[now] = 1;
    if( vis[now] == 0 ) vis[now] = 1;
    for( int i = 0; i < p[now].size(); i ++ ){
        int v = p[now][i];
        if( fl[v] == 1 ) continue;
        dele( v );
    }
}

void dfs( int now, int cnt )
{
    vis[now] = cnt;
    if( vis[ nxt[now] ] != 0 ){
        g.push_back( cnt-vis[ nxt[now] ]+1 );
        dele( now );
        return ;
    }
    dfs( nxt[now], cnt+1 );
}

int main()
{
    int n, i, j;
    init();
    scanf("%d", &n);
    for( i = 1; i <= n; i ++ ) scanf("%d", &nxt[i]), p[i].push_back( nxt[i] ), p[ nxt[i] ].push_back(i);
    for( i = 1; i <= n; i ++ ){
        if( vis[i] == 0 )
            dfs( i, 1 );
    }
    ll ans = 0;
    for( i = 0; i < g.size(); i ++ ) ans += g[i];
    //printf("%I64d %d\n", ans, g.size());
    ll res = f2[n-ans];
    for( i = 0; i < g.size(); i ++ ){
        res *= f2[g[i]]+mod-2;
        res %= mod;
    }
    printf("%I64d\n", res);
}


#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#define pii pair< ll, ll >
#define mp( a, b ) make_pair( a, b )
#define lson l, mid, x<<1
#define rson mid+1, r, x<<1|1
#define xx first
#define yy second
using namespace std;
typedef __int64 ll;
const ll mod = 1e9+7;

ll f2[200005];

void init()
{
    int i;
    f2[0] = 1;
    for( i = 1; i <= 200000; i ++ ){
        f2[i] = 1LL*f2[i-1]*2%mod;
    }
}

int nxt[200005];
int vis[200005];
bool fl[200005];

vector<ll> g;

void dele( int now )
{
    fl[now] = 1;
    if( fl[ nxt[now] ] == 1 ) return ;
    dele( nxt[now] );
}

void dfs( int now, int cnt )
{
    vis[now] = cnt;
    if( vis[ nxt[now] ] != 0 && fl[ nxt[now] ] == 0 ){
        g.push_back( cnt-vis[ nxt[now] ]+1 );
        dele( now );
        return ;
    }
    else if( vis[ nxt[now] ] != 0 ) return ;
    dfs( nxt[now], cnt+1 );
}

int main()
{
    int n, i, j;
    init();
    scanf("%d", &n);
    for( i = 1; i <= n; i ++ ) scanf("%d", &nxt[i]);
    for( i = 1; i <= n; i ++ ){
        if( vis[i] == 0 )
            dfs( i, 1 );
    }
    ll ans = 0;
    for( i = 0; i < g.size(); i ++ ) ans += g[i];
    //printf("%I64d %d\n", ans, g.size());
    ll res = f2[n-ans];
    for( i = 0; i < g.size(); i ++ ) printf("%I64d ", g[i]);
    for( i = 0; i < g.size(); i ++ ){
        res *= f2[g[i]]+mod-2;
        res %= mod;
    }
    printf("%I64d\n", res);
}


#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#define pii pair< ll, ll >
#define mp( a, b ) make_pair( a, b )
#define lson l, mid, x<<1
#define rson mid+1, r, x<<1|1
#define xx first
#define yy second
using namespace std;
typedef __int64 ll;
const ll mod = 1e9+7;

ll f2[200005];

void init()
{
    int i;
    f2[0] = 1;
    for( i = 1; i <= 200000; i ++ ){
        f2[i] = 1LL*f2[i-1]*2%mod;
    }
}

int nxt[200005];
int vis[200005];

ll num = 0;
void dfs( int now, int cnt )
{
    if( vis[ nxt[now] ] != 0 ){
        num = cnt-vis[ nxt[now] ];
        return ;
    }
    vis[now] = cnt;
    dfs( nxt[now], cnt+1 );
}

int main()
{
    int n, i, j;
    init();
    scanf("%d", &n);
    for( i = 1; i <= n; i ++ ) scanf("%d", &nxt[i]);
    dfs( 1, 1 );
    ll ans = 0;
    printf("%I64d\n", (f2[n]+2*mod-f2[n-num])%mod);
}


#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#define pii pair< ll, ll >
#define mp( a, b ) make_pair( a, b )
#define lson l, mid, x<<1
#define rson mid+1, r, x<<1|1
#define xx first
#define yy second
using namespace std;
typedef __int64 ll;
const ll mod = 1e9+7;

ll f2[200005];

void init()
{
    int i;
    f2[0] = 1;
    for( i = 1; i <= 200000; i ++ ){
        f2[i] = 1LL*f2[i-1]*2%mod;
    }
}

int nxt[200005];
int vis[200005];
bool fl[200005];

vector<ll> g;

void dele( int now )
{
    fl[now] = 1;
    if( fl[ nxt[now] ] == 1 ) return ;
    dele( nxt[now] );
}

void dfs( int now, int cnt )
{
    if( vis[ nxt[now] ] != 0 && fl[ nxt[now] ] == 0 ){
        g.push_back( cnt-vis[ nxt[now] ]+1 );
        dele( now );
        return ;
    }
    else if( vis[ nxt[now] ] != 0 ) return ;
    vis[now] = cnt;
    dfs( nxt[now], cnt+1 );
}

int main()
{
    int n, i, j;
    init();
    scanf("%d", &n);
    for( i = 1; i <= n; i ++ ) scanf("%d", &nxt[i]);
    for( i = 1; i <= n; i ++ ){
        if( vis[i] == 0 )
            dfs( i, 1 );
    }
    ll ans = 0;
    for( i = 0; i < g.size(); i ++ ) ans += g[i];
    //printf("%I64d %d\n", ans, g.size());
    ll res = f2[n-ans];
    for( i = 0; i < g.size(); i ++ ){
        res *= f2[g[i]]+mod-2;
        res %= mod;
    }
    printf("%I64d\n", res);
}


#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#define pii pair< ll, ll >
#define mp( a, b ) make_pair( a, b )
#define lson l, mid, x<<1
#define rson mid+1, r, x<<1|1
#define xx first
#define yy second
using namespace std;
typedef __int64 ll;
const ll mod = 1e9+7;

ll f2[200005];

void init()
{
    int i;
    f2[0] = 1;
    for( i = 1; i <= 200000; i ++ ){
        f2[i] = 1LL*f2[i-1]*2%mod;
    }
}

int nxt[200005];
int vis[200005];
bool fl[200005];

vector<ll> g;

void dele( int now )
{
    fl[now] = 1;
    if( fl[ nxt[now] ] == 1 ) return ;
    dele( nxt[now] );
}

void dfs( int now, int cnt )
{
    vis[now] = cnt;
    if( vis[ nxt[now] ] != 0 && fl[ nxt[now] ] == 0 ){
        g.push_back( cnt-vis[ nxt[now] ]+1 );
        dele( now );
        return ;
    }
    else if( vis[ nxt[now] ] != 0 ) return ;
    dfs( nxt[now], cnt+1 );
}

int main()
{
    int n, i, j;
    init();
    scanf("%d", &n);
    for( i = 1; i <= n; i ++ ) scanf("%d", &nxt[i]);
    for( i = 1; i <= n; i ++ ){
        if( vis[i] == 0 )
            dfs( i, 1 );
    }
    ll ans = 0;
    for( i = 0; i < g.size(); i ++ ) ans += g[i];
    //printf("%I64d %d\n", ans, g.size());
    ll res = f2[n-ans];
    for( i = 0; i < g.size(); i ++ ){
        res *= f2[g[i]]+mod-2;
        res %= mod;
    }
    printf("%I64d\n", res);
}


#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<vector>
#include<queue>
#include<map>
#define pii pair< ll, ll >
#define mp( a, b ) make_pair( a, b )
#define lson l, mid, x<<1
#define rson mid+1, r, x<<1|1
#define xx first
#define yy second
using namespace std;
typedef __int64 ll;
const ll mod = 1e9+7;

ll f2[200005];

void init()
{
    int i;
    f2[0] = 1;
    for( i = 1; i <= 200000; i ++ ){
        f2[i] = 1LL*f2[i-1]*2%mod;
    }
}

int nxt[200005];
int vis[200005];
bool fl[200005];

vector<ll> g;

void dele( int now )
{
    fl[now] = 1;
    if( fl[ nxt[now] ] == 1 ) return ;
    dele( nxt[now] );
}

void dfs( int now, int cnt )
{
    if( vis[ nxt[now] ] != 0 && fl[ nxt[now] ] == 0 ){
        g.push_back( cnt-vis[ nxt[now] ]+1 );
        dele( now );
        return ;
    }
    else if( vis[ nxt[now] ] != 0 ) return ;
    vis[now] = cnt;
    dfs( nxt[now], cnt+1 );
}

int main()
{
    int n, i, j;
    init();
    scanf("%d", &n);
    for( i = 1; i <= n; i ++ ) scanf("%d", &nxt[i]);
    for( i = 1; i <= n; i ++ ){
        if( vis[i] == 0 )
            dfs( i, 1 );
    }
    ll ans = 0;
    for( i = 0; i < g.size(); i ++ ) ans += g[i];
    //printf("%I64d %d\n", ans, g.size());
    ll res = f2[n-ans];
    for( i = 0; i < g.size(); i ++ ){
        res *= f2[g[i]]-2;
        res %= mod;
    }
    printf("%I64d\n", res);
}


/****************************
*  Author- Advitiya Brijesh *
*  PIE @ MNNIT Allahabad    *
*  Username-asymptote_      *
*                           *
****************************/
// WA TLE MLE RE IDLE and counting...
#include<bits/stdc++.h>
using namespace std;
//      std macros
typedef long long ll;
typedef vector<list<ll> > vli;
typedef vector<ll > vi;
typedef vector<pair<ll,ll> > vpll;
typedef vector<pair<int,int> > vpii;
typedef pair<int ,int> pii;
//      dereference
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define in insert
#define pu push
#define fr front
//      loops
#define rep(i,n) for(int i=0;i<n;++i)
#define REP(i,a,b) for(int i=a;i<=b;++i)
#define PER(i,b,a) for(int i=b;i>=a;--i)
#define all(X) (X).begin(), (X).end()
//      I/O
#define dsd(X) int (X); scanf("%d", &X)
#define dsd2(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define dsd3(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define sd(n) scanf("%d",&n)
#define sld(n) scanf("%ld",&n)
#define sll(n) scanf("%lld",&n)
#define sc(n) scanf("%c",&n)
#define ss(n) scanf("%s",n)
#define oll(n) printf("%lld\n",n)
//      set values
#define mset(n,k) memset(n,k,sizeof(n))
#define MOD 1000000007
ll INV2=500000004;
ll INV6=166666668;
//modular expo
ll power(ll a,ll b, ll c){
    ll x=1,y=a;
    while(b>0){
        if(b&1)
            x=(x*y)%c;
        y=(y*y)%c;
        b/=2;
    }
    return x%c;
}
//perfect numbers till 10^6 6, 28, 496, 8128
int dx[]={0,-1,0,1};
int dy[]={-1,0,1,0};//clockwise from left
vector<int>v[200005],re[200005];
stack<int>s;
bool vis[200005];
int assi[100005],ind[100005];
void toposort(int i){
	vis[i]=1;
	for(int j=0;j<v[i].size();++j){
		int idx=v[i][j];
		if(!vis[idx]){
			toposort(idx);
		}
	}
	s.push(i);
}
int val=1,n;
ll sz,sp;
void kosa(int i){
	vis[i]=1;
	sz++;
	for(int j=0;j<re[i].size();++j){
		int idx=re[i][j];
		if(!vis[idx]){
			kosa(idx);
		}
	}
}
int main(){

		sd(n);
		sp=n;
		int x,y;
		rep(i,n){
			sd(x);
			v[i+1].pb(x);
			re[x].pb(i+1);
		}
		REP(i,1,n){
			if(!vis[i]){
				toposort(i);
			}
		}
		REP(i,1,n){
			vis[i]=0;
		}
		int pre=-1,cc=0;
		vector<ll>anus;
		while(!s.empty()){
			int fi=s.top();
			if(!vis[fi]){
				sz=0;
				kosa(fi);
				if(sz>1)
					anus.pb(sz);
				if(sz>1)
					sp-=sz;
				cc++;
			}
			s.pop();
		}
		if(!cc){
			ll as=power(2,n,MOD);
			printf("%lld\n",as);
			return 0;
		}
		//cout<<"sp "<<sp<<endl;
		ll ans=1,mul=power(2,sp,MOD);
		for(int i=0;i<anus.size();++i){
			ll ass=anus[i];
			//cout<<"size "<<ass<<" ";
			ll dekh=power(2,ass,MOD);
			ll fin=(dekh-2+MOD)%MOD;
			fin=(fin*mul)%MOD;
			//cout<<"i "<<i<<" "<<fin<<endl;
			ans=(ans*fin)%MOD;
		}
		printf("%lld\n",ans);

return 0;}


/****************************
*  Author- Advitiya Brijesh *
*  PIE @ MNNIT Allahabad    *
*  Username-asymptote_      *
*                           *
****************************/
// WA TLE MLE RE IDLE and counting...
#include<bits/stdc++.h>
using namespace std;
//      std macros
typedef long long ll;
typedef vector<list<ll> > vli;
typedef vector<ll > vi;
typedef vector<pair<ll,ll> > vpll;
typedef vector<pair<int,int> > vpii;
typedef pair<int ,int> pii;
//      dereference
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define in insert
#define pu push
#define fr front
//      loops
#define rep(i,n) for(int i=0;i<n;++i)
#define REP(i,a,b) for(int i=a;i<=b;++i)
#define PER(i,b,a) for(int i=b;i>=a;--i)
#define all(X) (X).begin(), (X).end()
//      I/O
#define dsd(X) int (X); scanf("%d", &X)
#define dsd2(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define dsd3(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define sd(n) scanf("%d",&n)
#define sld(n) scanf("%ld",&n)
#define sll(n) scanf("%lld",&n)
#define sc(n) scanf("%c",&n)
#define ss(n) scanf("%s",n)
#define oll(n) printf("%lld\n",n)
//      set values
#define mset(n,k) memset(n,k,sizeof(n))
#define MOD 1000000007
ll INV2=500000004;
ll INV6=166666668;
//modular expo
ll power(ll a,ll b, ll c){
    ll x=1,y=a;
    while(b>0){
        if(b&1)
            x=(x*y)%c;
        y=(y*y)%c;
        b/=2;
    }
    return x%c;
}
//perfect numbers till 10^6 6, 28, 496, 8128
int dx[]={0,-1,0,1};
int dy[]={-1,0,1,0};//clockwise from left
vector<int>v[200005],re[200005];
stack<int>s;
bool vis[200005];
void toposort(int i){
	vis[i]=1;
	for(int j=0;j<v[i].size();++j){
		int idx=v[i][j];
		if(!vis[idx]){
			toposort(idx);
		}
	}
	s.push(i);
}
int val=1,n;
ll sz,sp;
void kosa(int i){
	vis[i]=1;
	sz++;
	for(int j=0;j<re[i].size();++j){
		int idx=re[i][j];
		if(!vis[idx]){
			kosa(idx);
		}
	}
}
int main(){

		sd(n);
		sp=n;
		int x,y;
		rep(i,n){
			sd(x);
			v[i+1].pb(x);
			re[x].pb(i+1);
		}
		REP(i,1,n){
			if(!vis[i]){
				toposort(i);
			}
		}
		REP(i,1,n){
			vis[i]=0;
		}
		int cc=0;
		int kit=0;
		vector<ll>anus;
		while(!s.empty()){
			int fi=s.top();
			if(!vis[fi]){
				sz=0;
				kosa(fi);
				if(sz>1){
					anus.pb(sz);
				}
				else{
					cc++;
				}
			}
			s.pop();
		}
		ll mul=power(2,cc,MOD);
		ll ans=1;
		for(int i=0;i<anus.size();++i){
			ll de=anus[i];
			ll fin=power(2,de,MOD);
			fin=(fin-2+MOD)%MOD;
			ans=(ans*fin)%MOD;
		}
		ans=(ans*mul)%MOD;
		printf("%lld\n",ans);

return 0;}


/****************************
*  Author- Advitiya Brijesh *
*  PIE @ MNNIT Allahabad    *
*  Username-asymptote_      *
*                           *
****************************/
// WA TLE MLE RE IDLE and counting...
#include<bits/stdc++.h>
using namespace std;
//      std macros
typedef long long ll;
typedef vector<list<ll> > vli;
typedef vector<ll > vi;
typedef vector<pair<ll,ll> > vpll;
typedef vector<pair<int,int> > vpii;
typedef pair<int ,int> pii;
//      dereference
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define in insert
#define pu push
#define fr front
//      loops
#define rep(i,n) for(int i=0;i<n;++i)
#define REP(i,a,b) for(int i=a;i<=b;++i)
#define PER(i,b,a) for(int i=b;i>=a;--i)
#define all(X) (X).begin(), (X).end()
//      I/O
#define dsd(X) int (X); scanf("%d", &X)
#define dsd2(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define dsd3(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define sd(n) scanf("%d",&n)
#define sld(n) scanf("%ld",&n)
#define sll(n) scanf("%lld",&n)
#define sc(n) scanf("%c",&n)
#define ss(n) scanf("%s",n)
#define oll(n) printf("%lld\n",n)
//      set values
#define mset(n,k) memset(n,k,sizeof(n))
#define MOD 1000000007
ll INV2=500000004;
ll INV6=166666668;
//modular expo
ll power(ll a,ll b, ll c){
    ll x=1,y=a;
    while(b>0){
        if(b&1)
            x=(x*y)%c;
        y=(y*y)%c;
        b/=2;
    }
    return x%c;
}
//perfect numbers till 10^6 6, 28, 496, 8128
int dx[]={0,-1,0,1};
int dy[]={-1,0,1,0};//clockwise from left
vector<int>v[100005],re[100005];
stack<int>s;
bool vis[100005];
int assi[100005],ind[100005];
void toposort(int i){
	vis[i]=1;
	for(int j=0;j<v[i].size();++j){
		int idx=v[i][j];
		if(!vis[idx]){
			toposort(idx);
		}
	}
	s.push(i);
}
int val=1,n;
ll sz,sp;
void kosa(int i){
	vis[i]=1;
	sz++;
	for(int j=0;j<re[i].size();++j){
		int idx=re[i][j];
		if(!vis[idx]){
			kosa(idx);
		}
	}
}
int main(){

		sd(n);
		sp=n;
		int x,y;
		rep(i,n){
			sd(x);
			v[i+1].pb(x);
			re[x].pb(i+1);
		}
		REP(i,1,n){
			if(!vis[i]){
				toposort(i);
			}
		}
		REP(i,1,n){
			vis[i]=0;
		}
		int pre=-1,cc=0;
		vector<ll>anus;
		while(!s.empty()){
			int fi=s.top();
			if(!vis[fi]){
				sz=0;
				kosa(fi);
				anus.pb(sz);
				if(sz>1)
					sp-=sz;
				cc++;
			}
			s.pop();
		}
		if(!cc){
			ll as=power(2,n,MOD);
			printf("%lld\n",as);
			return 0;
		}
		//cout<<"sp "<<sp<<endl;
		ll ans=0,mul=power(2,sp,MOD);
		for(int i=0;i<anus.size();++i){
			ll ass=anus[i];
			ll dekh=power(2,ass,MOD);
			ll fin=(dekh-2+MOD)%MOD;
			fin=(fin*mul)%MOD;
			ans=(ans+fin)%MOD;
		}
		printf("%lld\n",ans);

return 0;}


/****************************
*  Author- Advitiya Brijesh *
*  PIE @ MNNIT Allahabad    *
*  Username-asymptote_      *
*                           *
****************************/
// WA TLE MLE RE IDLE and counting...
#include<bits/stdc++.h>
using namespace std;
//      std macros
typedef long long ll;
typedef vector<list<ll> > vli;
typedef vector<ll > vi;
typedef vector<pair<ll,ll> > vpll;
typedef vector<pair<int,int> > vpii;
typedef pair<int ,int> pii;
//      dereference
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define in insert
#define pu push
#define fr front
//      loops
#define rep(i,n) for(int i=0;i<n;++i)
#define REP(i,a,b) for(int i=a;i<=b;++i)
#define PER(i,b,a) for(int i=b;i>=a;--i)
#define all(X) (X).begin(), (X).end()
//      I/O
#define dsd(X) int (X); scanf("%d", &X)
#define dsd2(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define dsd3(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define sd(n) scanf("%d",&n)
#define sld(n) scanf("%ld",&n)
#define sll(n) scanf("%lld",&n)
#define sc(n) scanf("%c",&n)
#define ss(n) scanf("%s",n)
#define oll(n) printf("%lld\n",n)
//      set values
#define mset(n,k) memset(n,k,sizeof(n))
#define MOD 1000000007
ll INV2=500000004;
ll INV6=166666668;
//modular expo
ll power(ll a,ll b, ll c){
    ll x=1,y=a;
    while(b>0){
        if(b&1)
            x=(x*y)%c;
        y=(y*y)%c;
        b/=2;
    }
    return x%c;
}
//perfect numbers till 10^6 6, 28, 496, 8128
int dx[]={0,-1,0,1};
int dy[]={-1,0,1,0};//clockwise from left
vector<int>v[200005],re[200005];
stack<int>s;
bool vis[200005];
void toposort(int i){
	vis[i]=1;
	for(int j=0;j<v[i].size();++j){
		int idx=v[i][j];
		if(!vis[idx]){
			toposort(idx);
		}
	}
	s.push(i);
}
int val=1,n;
ll sz,sp;
void kosa(int i){
	vis[i]=1;
	sz++;
	for(int j=0;j<re[i].size();++j){
		int idx=re[i][j];
		if(!vis[idx]){
			kosa(idx);
		}
	}
}
int main(){

		sd(n);
		sp=n;
		int x,y;
		rep(i,n){
			sd(x);
			v[i+1].pb(x);
			re[x].pb(i+1);
		}
		REP(i,1,n){
			if(!vis[i]){
				toposort(i);
			}
		}
		REP(i,1,n){
			vis[i]=0;
		}
		int cc=0;
		while(!s.empty()){
			int fi=s.top();
			if(!vis[fi]){
				sz=0;
				kosa(fi);
				if(sz>1)
					sp=sz;
				else{
					cc++;
				}
			}
			s.pop();
		}

		ll ans=1LL,mul=power(2LL,cc,MOD);
		ll va=power(2LL,n-cc,MOD);
		va=(va+2LL*MOD-2LL)%MOD;
		ans=(va*mul)%MOD;
		printf("%lld\n",ans);

return 0;}


/****************************
*  Author- Advitiya Brijesh *
*  PIE @ MNNIT Allahabad    *
*  Username-asymptote_      *
*                           *
****************************/
// WA TLE MLE RE IDLE and counting...
#include<bits/stdc++.h>
using namespace std;
//      std macros
typedef long long ll;
typedef vector<list<ll> > vli;
typedef vector<ll > vi;
typedef vector<pair<ll,ll> > vpll;
typedef vector<pair<int,int> > vpii;
typedef pair<int ,int> pii;
//      dereference
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define in insert
#define pu push
#define fr front
//      loops
#define rep(i,n) for(int i=0;i<n;++i)
#define REP(i,a,b) for(int i=a;i<=b;++i)
#define PER(i,b,a) for(int i=b;i>=a;--i)
#define all(X) (X).begin(), (X).end()
//      I/O
#define dsd(X) int (X); scanf("%d", &X)
#define dsd2(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define dsd3(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define sd(n) scanf("%d",&n)
#define sld(n) scanf("%ld",&n)
#define sll(n) scanf("%lld",&n)
#define sc(n) scanf("%c",&n)
#define ss(n) scanf("%s",n)
#define oll(n) printf("%lld\n",n)
//      set values
#define mset(n,k) memset(n,k,sizeof(n))
#define MOD 1000000007
ll INV2=500000004;
ll INV6=166666668;
//modular expo
ll power(ll a,ll b, ll c){
    ll x=1,y=a;
    while(b>0){
        if(b&1)
            x=(x*y)%c;
        y=(y*y)%c;
        b/=2;
    }
    return x%c;
}
//perfect numbers till 10^6 6, 28, 496, 8128
int dx[]={0,-1,0,1};
int dy[]={-1,0,1,0};//clockwise from left
vector<int>v[200005],re[200005];
stack<int>s;
bool vis[200005];
void toposort(int i){
	vis[i]=1;
	for(int j=0;j<v[i].size();++j){
		int idx=v[i][j];
		if(!vis[idx]){
			toposort(idx);
		}
	}
	s.push(i);
}
int val=1,n;
ll sz,sp;
void kosa(int i){
	vis[i]=1;
	sz++;
	for(int j=0;j<re[i].size();++j){
		int idx=re[i][j];
		if(!vis[idx]){
			kosa(idx);
		}
	}
}
int main(){

		sd(n);
		sp=n;
		int x,y;
		rep(i,n){
			sd(x);
			v[i+1].pb(x);
			re[x].pb(i+1);
		}
		REP(i,1,n){
			if(!vis[i]){
				toposort(i);
			}
		}
		REP(i,1,n){
			vis[i]=0;
		}
		int cc=0;
		while(!s.empty()){
			int fi=s.top();
			if(!vis[fi]){
				sz=0;
				kosa(fi);
				if(sz>1)
					sp=sz;
				cc++;
			}
			s.pop();
		}
		if(!cc){
			ll as=power(2,n,MOD);
			printf("%lld\n",as);
			return 0;
		}
		//cout<<"sp "<<sp<<endl;
		ll ans=1LL,mul=power(2LL,n-sp,MOD);
		ll va=power(2LL,sp,MOD);
		va=(va+2LL*MOD-2)%MOD;
		ans=(va*mul)%MOD;
		printf("%lld\n",ans);

return 0;}


/****************************
*  Author- Advitiya Brijesh *
*  PIE @ MNNIT Allahabad    *
*  Username-asymptote_      *
*                           *
****************************/
// WA TLE MLE RE IDLE and counting...
#include<bits/stdc++.h>
using namespace std;
//      std macros
typedef long long ll;
typedef vector<list<ll> > vli;
typedef vector<ll > vi;
typedef vector<pair<ll,ll> > vpll;
typedef vector<pair<int,int> > vpii;
typedef pair<int ,int> pii;
//      dereference
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define in insert
#define pu push
#define fr front
//      loops
#define rep(i,n) for(int i=0;i<n;++i)
#define REP(i,a,b) for(int i=a;i<=b;++i)
#define PER(i,b,a) for(int i=b;i>=a;--i)
#define all(X) (X).begin(), (X).end()
//      I/O
#define dsd(X) int (X); scanf("%d", &X)
#define dsd2(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define dsd3(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define sd(n) scanf("%d",&n)
#define sld(n) scanf("%ld",&n)
#define sll(n) scanf("%lld",&n)
#define sc(n) scanf("%c",&n)
#define ss(n) scanf("%s",n)
#define oll(n) printf("%lld\n",n)
//      set values
#define mset(n,k) memset(n,k,sizeof(n))
#define MOD 1000000007
ll INV2=500000004;
ll INV6=166666668;
//modular expo
ll power(ll a,ll b, ll c){
    ll x=1,y=a;
    while(b>0){
        if(b&1)
            x=(x*y)%c;
        y=(y*y)%c;
        b/=2;
    }
    return x%c;
}
//perfect numbers till 10^6 6, 28, 496, 8128
int dx[]={0,-1,0,1};
int dy[]={-1,0,1,0};//clockwise from left
vector<int>v[200005],re[200005];
stack<int>s;
bool vis[200005];
int assi[100005],ind[100005];
void toposort(int i){
	vis[i]=1;
	for(int j=0;j<v[i].size();++j){
		int idx=v[i][j];
		if(!vis[idx]){
			toposort(idx);
		}
	}
	s.push(i);
}
int val=1,n;
ll sz,sp;
void kosa(int i){
	vis[i]=1;
	sz++;
	for(int j=0;j<re[i].size();++j){
		int idx=re[i][j];
		if(!vis[idx]){
			kosa(idx);
		}
	}
}
int main(){

		sd(n);
		sp=n;
		int x,y;
		rep(i,n){
			sd(x);
			v[i+1].pb(x);
			re[x].pb(i+1);
		}
		REP(i,1,n){
			if(!vis[i]){
				toposort(i);
			}
		}
		REP(i,1,n){
			vis[i]=0;
		}
		int pre=-1,cc=0;
		vector<ll>anus;
		anus.clear();
		while(!s.empty()){
			int fi=s.top();
			if(!vis[fi]){
				sz=0;
				kosa(fi);
				if(sz>1)
					anus.pb(sz);
				if(sz>1)
					sp-=sz;
				cc++;
			}
			s.pop();
		}
		if(!cc){
			ll as=power(2,n,MOD);
			printf("%lld\n",as);
			return 0;
		}
		//cout<<"sp "<<sp<<endl;
		ll ans=1,mul=power(2,sp,MOD);
		ll bach=n-sp;
		ll va=power(2,bach,MOD);
		va=(va+MOD-2)%MOD;
		ans=(va*mul)%MOD;
		printf("%lld\n",ans);

return 0;}


/****************************
*  Author- Advitiya Brijesh *
*  PIE @ MNNIT Allahabad    *
*  Username-asymptote_      *
*                           *
****************************/
// WA TLE MLE RE IDLE and counting...
#include<bits/stdc++.h>
using namespace std;
//      std macros
typedef long long ll;
typedef vector<list<ll> > vli;
typedef vector<ll > vi;
typedef vector<pair<ll,ll> > vpll;
typedef vector<pair<int,int> > vpii;
typedef pair<int ,int> pii;
//      dereference
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define in insert
#define pu push
#define fr front
//      loops
#define rep(i,n) for(int i=0;i<n;++i)
#define REP(i,a,b) for(int i=a;i<=b;++i)
#define PER(i,b,a) for(int i=b;i>=a;--i)
#define all(X) (X).begin(), (X).end()
//      I/O
#define dsd(X) int (X); scanf("%d", &X)
#define dsd2(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define dsd3(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define sd(n) scanf("%d",&n)
#define sld(n) scanf("%ld",&n)
#define sll(n) scanf("%lld",&n)
#define sc(n) scanf("%c",&n)
#define ss(n) scanf("%s",n)
#define oll(n) printf("%lld\n",n)
//      set values
#define mset(n,k) memset(n,k,sizeof(n))
#define MOD 1000000007
ll INV2=500000004;
ll INV6=166666668;
//modular expo
ll power(ll a,ll b, ll c){
    ll x=1,y=a;
    while(b>0){
        if(b&1)
            x=(x*y)%c;
        y=(y*y)%c;
        b/=2;
    }
    return x%c;
}
//perfect numbers till 10^6 6, 28, 496, 8128
int dx[]={0,-1,0,1};
int dy[]={-1,0,1,0};//clockwise from left
vector<int>v[100005],re[100005];
stack<int>s;
bool vis[100005];
int assi[100005],ind[100005];
void toposort(int i){
	vis[i]=1;
	for(int j=0;j<v[i].size();++j){
		int idx=v[i][j];
		if(!vis[idx]){
			toposort(idx);
		}
	}
	s.push(i);
}
int val=1;
ll sz;
void kosa(int i){
	vis[i]=1;
	sz++;
	for(int j=0;j<re[i].size();++j){
		int idx=re[i][j];
		if(!vis[idx]){
			kosa(idx);
		}
	}
}
int main(){

	int n,m;

		sd(n);

		int x,y;
		rep(i,n){
			sd(x);
			v[i+1].pb(x);
			re[x].pb(i+1);
		}
		REP(i,1,n){
			if(!vis[i]){
				toposort(i);
			}
		}
		REP(i,1,n){
			vis[i]=0;
		}
		int pre=-1,cc=0;
		vector<ll>anus;
		while(!s.empty()){
			int fi=s.top();
			if(!vis[fi]){
				sz=0;
				kosa(fi);
				anus.pb(sz);
				cc++;
			}
			s.pop();
		}
		if(!cc){
			ll as=power(2,n,MOD);
			printf("%lld\n",as);
			return 0;
		}
		ll ans=0,mul=power(2,cc-1,MOD);
		for(int i=0;i<anus.size();++i){
			ll ass=anus[i];
			ll dekh=power(2,ass,MOD);
			ll fin=(dekh-2+MOD)%MOD;
			fin=(fin*mul)%MOD;
			ans=(ans+fin)%MOD;
		}
		printf("%lld\n",ans);

return 0;}


/****************************
*  Author- Advitiya Brijesh *
*  PIE @ MNNIT Allahabad    *
*  Username-asymptote_      *
*                           *
****************************/
// WA TLE MLE RE IDLE and counting...
#include<bits/stdc++.h>
using namespace std;
//      std macros
typedef long long ll;
typedef vector<list<ll> > vli;
typedef vector<ll > vi;
typedef vector<pair<ll,ll> > vpll;
typedef vector<pair<int,int> > vpii;
typedef pair<int ,int> pii;
//      dereference
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define in insert
#define pu push
#define fr front
//      loops
#define rep(i,n) for(int i=0;i<n;++i)
#define REP(i,a,b) for(int i=a;i<=b;++i)
#define PER(i,b,a) for(int i=b;i>=a;--i)
#define all(X) (X).begin(), (X).end()
//      I/O
#define dsd(X) int (X); scanf("%d", &X)
#define dsd2(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define dsd3(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define sd(n) scanf("%d",&n)
#define sld(n) scanf("%ld",&n)
#define sll(n) scanf("%lld",&n)
#define sc(n) scanf("%c",&n)
#define ss(n) scanf("%s",n)
#define oll(n) printf("%lld\n",n)
//      set values
#define mset(n,k) memset(n,k,sizeof(n))
#define MOD 1000000007
ll INV2=500000004;
ll INV6=166666668;
//modular expo
ll power(ll a,ll b, ll c){
    ll x=1,y=a;
    while(b>0){
        if(b&1)
            x=(x*y)%c;
        y=(y*y)%c;
        b/=2;
    }
    return x%c;
}
//perfect numbers till 10^6 6, 28, 496, 8128
int dx[]={0,-1,0,1};
int dy[]={-1,0,1,0};//clockwise from left
vector<int>v[200005],re[200005];
stack<int>s;
bool vis[200005];
void toposort(int i){
	vis[i]=1;
	for(int j=0;j<v[i].size();++j){
		int idx=v[i][j];
		if(!vis[idx]){
			toposort(idx);
		}
	}
	s.push(i);
}
int val=1,n;
ll sz,sp;
void kosa(int i){
	vis[i]=1;
	sz++;
	for(int j=0;j<re[i].size();++j){
		int idx=re[i][j];
		if(!vis[idx]){
			kosa(idx);
		}
	}
}
int main(){

		sd(n);
		sp=n;
		int x,y;
		rep(i,n){
			sd(x);
			v[i+1].pb(x);
			re[x].pb(i+1);
		}
		REP(i,1,n){
			if(!vis[i]){
				toposort(i);
			}
		}
		REP(i,1,n){
			vis[i]=0;
		}
		int pre=-1,cc=0;
		vector<ll>anus;
		while(!s.empty()){
			int fi=s.top();
			if(!vis[fi]){
				sz=0;
				kosa(fi);
				anus.pb(sz);
				if(sz>1)
					sp-=sz;
				cc++;
			}
			s.pop();
		}
		if(!cc){
			ll as=power(2,n,MOD);
			printf("%lld\n",as);
			return 0;
		}
		//cout<<"sp "<<sp<<endl;
		ll ans=0,mul=power(2,sp,MOD);
		for(int i=0;i<anus.size();++i){
			ll ass=anus[i];
			ll dekh=power(2,ass,MOD);
			ll fin=(dekh-2+MOD)%MOD;
			fin=(fin*mul)%MOD;
			ans=(ans+fin)%MOD;
		}
		printf("%lld\n",ans);

return 0;}


/****************************
*  Author- Advitiya Brijesh *
*  PIE @ MNNIT Allahabad    *
*  Username-asymptote_      *
*                           *
****************************/
// WA TLE MLE RE IDLE and counting...
#include<bits/stdc++.h>
using namespace std;
//      std macros
typedef long long ll;
typedef vector<list<ll> > vli;
typedef vector<ll > vi;
typedef vector<pair<ll,ll> > vpll;
typedef vector<pair<int,int> > vpii;
typedef pair<int ,int> pii;
//      dereference
#define F first
#define S second
#define pb push_back
#define mp make_pair
#define in insert
#define pu push
#define fr front
//      loops
#define rep(i,n) for(int i=0;i<n;++i)
#define REP(i,a,b) for(int i=a;i<=b;++i)
#define PER(i,b,a) for(int i=b;i>=a;--i)
#define all(X) (X).begin(), (X).end()
//      I/O
#define dsd(X) int (X); scanf("%d", &X)
#define dsd2(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define dsd3(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define sd(n) scanf("%d",&n)
#define sld(n) scanf("%ld",&n)
#define sll(n) scanf("%lld",&n)
#define sc(n) scanf("%c",&n)
#define ss(n) scanf("%s",n)
#define oll(n) printf("%lld\n",n)
//      set values
#define mset(n,k) memset(n,k,sizeof(n))
#define MOD 1000000007
ll INV2=500000004;
ll INV6=166666668;
//modular expo
ll power(ll a,ll b, ll c){
    ll x=1,y=a;
    while(b>0){
        if(b&1)
            x=(x*y)%c;
        y=(y*y)%c;
        b/=2;
    }
    return x%c;
}
//perfect numbers till 10^6 6, 28, 496, 8128
int dx[]={0,-1,0,1};
int dy[]={-1,0,1,0};//clockwise from left
vector<int>v[200005],re[200005];
stack<int>s;
bool vis[200005];
int assi[100005],ind[100005];
void toposort(int i){
	vis[i]=1;
	for(int j=0;j<v[i].size();++j){
		int idx=v[i][j];
		if(!vis[idx]){
			toposort(idx);
		}
	}
	s.push(i);
}
int val=1,n;
ll sz,sp;
void kosa(int i){
	vis[i]=1;
	sz++;
	for(int j=0;j<re[i].size();++j){
		int idx=re[i][j];
		if(!vis[idx]){
			kosa(idx);
		}
	}
}
int main(){

		sd(n);
		sp=n;
		int x,y;
		rep(i,n){
			sd(x);
			v[i+1].pb(x);
			re[x].pb(i+1);
		}
		REP(i,1,n){
			if(!vis[i]){
				toposort(i);
			}
		}
		REP(i,1,n){
			vis[i]=0;
		}
		int pre=-1,cc=0;
		vector<ll>anus;
		while(!s.empty()){
			int fi=s.top();
			if(!vis[fi]){
				sz=0;
				kosa(fi);
				if(sz>1)
					anus.pb(sz);
				if(sz>1)
					sp-=sz;
				cc++;
			}
			s.pop();
		}
		if(!cc){
			ll as=power(2,n,MOD);
			printf("%lld\n",as);
			return 0;
		}
		//cout<<"sp "<<sp<<endl;
		ll ans=1,mul=power(2,cc-1,MOD);
		for(int i=0;i<anus.size();++i){
			ll ass=anus[i];
			//cout<<"size "<<ass<<" ";
			ll dekh=power(2,ass,MOD);
			ll fin=(dekh-2+MOD)%MOD;
			fin=(fin*mul)%MOD;
			//cout<<"i "<<i<<" "<<fin<<endl;
			ans=(ans*fin)%MOD;
		}
		printf("%lld\n",ans);

return 0;}


#include<bits/stdc++.h>
using namespace std;

#define endl '\n'
#define D(x) cout << #x << " = " << (x) << endl;
#define IO ios_base::sync_with_stdio(false); cin.tie(NULL);

const int mx = 200100;
const int mod = 1e9 + 7;

long long mpow (long long b, long long p, long long m) {
  long long mask = 1;
  long long pow2 = b % m;
  long long r = 1;

  while (mask) {
    if (p & mask) r = (r * pow2) % m;
    pow2 = (pow2 * pow2) % m;
    mask <<= 1;
  }

  return r;
}

void dfs (int i, vector<vector<int>> &G, vector<int> &visited, int &s) {
  if (!visited[i]) {
    visited[i] = true;
    s ++;

    for (int j = 0; j < G[i].size(); ++j) {
      dfs(G[i][j], G, visited, s);
    }
  }
}

struct edge {
  int start, end, weight;
  edge(){} edge(int start, int end, int weight) : start(start), end(end), weight(weight) {}
  bool operator < (const edge &that) const {
    // Change < by > to find maximum spanning tree
    return weight > that.weight;
  }
};

int p[mx];

void init_set (int &n) {
  for (int i = 0; i <= n; ++i) p[i] = i;
}

int find_set (int x) {
  return p[x] == x ? p[x] : p[x] = find_set(p[x]);
}

void join (int x, int y) {
  p[find_set(x)] = find_set(y);
}

void solve (vector<edge> &e, vector<vector<int>> &G, int &n) {
  init_set(n);

  long long ans = 1;
  int tot = 0;

  vector<int> visited(n, 0);

  for (int i = 0 ; i < e.size(); ++i) {
    int u = e[i].start;
    int v = e[i].end;
    if (find_set(u) != find_set(v)) {
      join(u, v);
    }
    else {
      int size = 0;
      dfs(u, G, visited, size);
      tot += size;
      long long tmp = mpow(2, size, mod);
      ans *= tmp - 2;
      if (tmp - 2 <= 0) assert(false);
    }
  }

  ans *= mpow(2, n - tot, mod);
  cout << ans % mod << endl;
}

int main() { IO
  int n;
  cin >> n;

  vector<edge> G;
  vector<vector<int>> G2(n);
  for (int i = 0; i < n; ++i) {
    int b;
    cin >> b; b --;

    edge e;
    e.start = i;
    e.end = b;

    G.push_back(e);
    G2[i].push_back(b);
  }

  solve(G, G2, n);
  return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define endl '\n'
#define D(x) cout << #x << " = " << (x) << endl;
#define IO ios_base::sync_with_stdio(false); cin.tie(NULL);

const int mx = 200100;
const int mod = 1e9 + 7;

long long mpow (long long b, long long p, long long m) {
  long long mask = 1;
  long long pow2 = b % m;
  long long r = 1;

  while (mask) {
    if (p & mask) r = (r * pow2) % m;
    pow2 = (pow2 * pow2) % m;
    mask <<= 1;
  }

  return r;
}

void dfs (int i, vector<vector<int>> &G, vector<int> &visited, int &s) {
  if (!visited[i]) {
    visited[i] = true;
    s ++;

    for (int j = 0; j < G[i].size(); ++j) {
      dfs(G[i][j], G, visited, s);
    }
  }
}

struct edge {
  int start, end, weight;
  edge(){} edge(int start, int end, int weight) : start(start), end(end), weight(weight) {}
  bool operator < (const edge &that) const {
    // Change < by > to find maximum spanning tree
    return weight > that.weight;
  }
};

int p[mx];

void init_set (int &n) {
  for (int i = 0; i <= n; ++i) p[i] = i;
}

int find_set (int x) {
  return p[x] == x ? p[x] : p[x] = find_set(p[x]);
}

void join (int x, int y) {
  p[find_set(x)] = find_set(y);
}

void solve (vector<edge> &e, vector<vector<int>> &G, int &n) {
  init_set(n);

  long long ans = 1;
  int tot = 0;

  vector<int> visited(n, 0);

  for (int i = 0 ; i < e.size(); ++i) {
    int u = e[i].start;
    int v = e[i].end;
    if (find_set(u) != find_set(v)) {
      join(u, v);
    }
    else {
      int size = 0;
      dfs(u, G, visited, size);
      tot += size;
      ans *= mpow(2, size, mod) - 2;
    }
  }

  ans *= mpow(2, n - tot, mod);
  cout << ans % mod << endl;
}

int main() { IO
  int n;
  cin >> n;

  vector<edge> G;
  vector<vector<int>> G2(n);
  for (int i = 0; i < n; ++i) {
    int b;
    cin >> b; b --;

    edge e;
    e.start = i;
    e.end = b;

    G.push_back(e);
    G2[i].push_back(b);
  }

  solve(G, G2, n);
  return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define endl '\n'
#define D(x) cout << #x << " = " << (x) << endl;
#define IO ios_base::sync_with_stdio(false); cin.tie(NULL);

/** Krustal implementation
 *  Complexity: O(E log V)
 **/

const int mx = 200100;
const int mod = 1e9 + 7;

struct edge {
  int start, end, weight;
  edge(){} edge(int start, int end, int weight) : start(start), end(end), weight(weight) {}
  bool operator < (const edge &that) const {
    // Change < by > to find maximum spanning tree
    return weight > that.weight;
  }
};

/** Begin union find
 *  Complexity: O(m log n) where m is the number of the operation
 *              and n are the number of objects, in practice is almost O(m)
 **/
int p[mx];

void init_set (int &n) {
  for (int i = 0; i <= n; ++i) p[i] = i;
}

int find_set (int x) {
  return p[x] == x ? p[x] : p[x] = find_set(p[x]);
}

void join (int x, int y) {
  p[find_set(x)] = find_set(y);
}
/**  End union find **/

/** e is a vector with all edges of the graph
 *  return graph of MST
 **/
void krustal (vector<edge> &e, vector<vector<int>> &G, int &n) {
  init_set(n);

  long long ans = 1;
  int size = 0;

  vector<int> visited(n, 0);
  for (int i = 0 ; i < e.size(); ++i) {
    int u = e[i].start;
    int v = e[i].end;
    if (find_set(u) != find_set(v)) {
      join(u, v);
    }
    else {
      int cur = u;
      vector<int> visited(n, 0);
      while (true) {
        if (!visited[cur]) {
          visited[cur] = true;
          if (G[cur].size() > 0) {
            cur = G[cur][0];
            size ++;
            ans *= 2;
            ans %= mod;
          }
          continue;
        }
        break;
      }

      break;
    }
  }

  if (size > 0) {
    ans -= 2;
  }

  int lim = n - size;
  for (int i = 0; i < lim; ++i) {
    ans *= 2;
    ans %= mod;
  }

  cout << ans % mod << endl;
}

int main() { IO
  int n;
  cin >> n;

  vector<edge> G;
  vector<vector<int>> G2(n);
  for (int i = 0; i < n; ++i) {
    int b;
    cin >> b; b --;

    edge e;
    e.start = i;
    e.end = b;

    G2[i].push_back(b);
    G.push_back(e);
  }

  krustal(G, G2, n);

  return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define endl '\n'
#define D(x) cout << #x << " = " << (x) << endl;
#define IO ios_base::sync_with_stdio(false); cin.tie(NULL);

const int mx = 200100;
const int mod = 1e9 + 7;

long long mpow (long long b, long long e, long long mod) {
  long long ans = 1;
  while (e) {
    if (e & 1) ans = (ans * b) % mod;
    b = (b * b) % mod;
    e >>= 1;
  }
  return ans;
}

void dfs (int i, vector<vector<int>> &G, vector<int> &visited, int &s) {
  if (!visited[i]) {
    visited[i] = true;
    s ++;

    for (int j = 0; j < G[i].size(); ++j) {
      dfs(G[i][j], G, visited, s);
    }
  }
}

struct edge {
  int start, end, weight;
  edge(){} edge(int start, int end, int weight) : start(start), end(end), weight(weight) {}
  bool operator < (const edge &that) const {
    // Change < by > to find maximum spanning tree
    return weight > that.weight;
  }
};

int p[mx];

void init_set (int &n) {
  for (int i = 0; i <= n; ++i) p[i] = i;
}

int find_set (int x) {
  return p[x] == x ? p[x] : p[x] = find_set(p[x]);
}

void join (int x, int y) {
  p[find_set(x)] = find_set(y);
}

void solve (vector<edge> &e, vector<vector<int>> &G, int &n) {
  init_set(n);

  long long ans = 1;
  int tot = 0;

  vector<int> visited(n, 0);

  for (int i = 0 ; i < e.size(); ++i) {
    int u = e[i].start;
    int v = e[i].end;
    if (find_set(u) != find_set(v)) {
      join(u, v);
    }
    else {
      int size = 0;
      dfs(u, G, visited, size);
      tot += size;
      long long tmp = mpow(2, size, mod);
      ans *= tmp - 2;
      if (tmp - 2 <= 0) assert(false);
    }
  }

  ans *= mpow(2, n - tot, mod);
  cout << ans % mod << endl;
}

int main() { IO
  int n;
  cin >> n;

  vector<edge> G;
  vector<vector<int>> G2(n);
  for (int i = 0; i < n; ++i) {
    int b;
    cin >> b; b --;

    edge e;
    e.start = i;
    e.end = b;

    G.push_back(e);
    G2[i].push_back(b);
  }

  solve(G, G2, n);
  return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define endl '\n'
#define D(x) cout << #x << " = " << (x) << endl;
#define IO ios_base::sync_with_stdio(false); cin.tie(NULL);

int mpow (int b, int p, int m) {
  int mask = 1;
  int pow2 = b % m;
  int r = 1;

  while (mask) {
    if (p & mask) r = (r * pow2) % m;
    pow2 = (pow2 * pow2) % m;
    mask <<= 1;
  }

  return r;
}

void dfs (int i, vector<vector<int>> &G, vector<int> &visited, vector<int> &visited2, int &s) {
  if (!visited[i]) {
    visited[i] = true;
    if (!visited2[i]) s ++;
    for (int j = 0; j < G[i].size(); ++j) {
      dfs(G[i][j], G, visited, visited2, s);
    }
  }
}

/** Krustal implementation
 *  Complexity: O(E log V)
 **/

const int mx = 200100;
const int mod = 1e9 + 7;

struct edge {
  int start, end, weight;
  edge(){} edge(int start, int end, int weight) : start(start), end(end), weight(weight) {}
  bool operator < (const edge &that) const {
    // Change < by > to find maximum spanning tree
    return weight > that.weight;
  }
};

/** Begin union find
 *  Complexity: O(m log n) where m is the number of the operation
 *              and n are the number of objects, in practice is almost O(m)
 **/
int p[mx];

void init_set (int &n) {
  for (int i = 0; i <= n; ++i) p[i] = i;
}

int find_set (int x) {
  return p[x] == x ? p[x] : p[x] = find_set(p[x]);
}

void join (int x, int y) {
  p[find_set(x)] = find_set(y);
}
/**  End union find **/

/** e is a vector with all edges of the graph
 *  return graph of MST
 **/
void krustal (vector<edge> &e, vector<vector<int>> &G, int &n) {
  init_set(n);

  long long ans = 1;
  int tot = 0;
  for (int i = 0 ; i < e.size(); ++i) {
    int u = e[i].start;
    int v = e[i].end;
    if (find_set(u) != find_set(v)) {
      join(u, v);
    }
    else {
      vector<int> visited(n, 0);
      int size = 0;
      int cur = u;
      while (true) {
        if (!visited[cur]) {
          size ++;
          visited[cur] = true;
          cur = G[cur][0];
        }
        else break;
      }
      tot += size;
      ans *= mpow(2, size, mod) - 2;
    }
  }

  ans *= mpow(2, n - tot, mod);
  cout << ans << endl;
}

int main() { IO
  int n;
  cin >> n;

  vector<edge> G;
  vector<vector<int>> G2(n);
  for (int i = 0; i < n; ++i) {
    int b;
    cin >> b; b --;

    edge e;
    e.start = i;
    e.end = b;

    G.push_back(e);

    G2[i].push_back(b);
    G2[b].push_back(i);
  }

  krustal(G, G2, n);
  return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define endl '\n'
#define D(x) cout << #x << " = " << (x) << endl;
#define IO ios_base::sync_with_stdio(false); cin.tie(NULL);

/** Krustal implementation
 *  Complexity: O(E log V)
 **/

const int mx = 200100;
const int mod = 1e9 + 7;

struct edge {
  int start, end, weight;
  edge(){} edge(int start, int end, int weight) : start(start), end(end), weight(weight) {}
  bool operator < (const edge &that) const {
    // Change < by > to find maximum spanning tree
    return weight > that.weight;
  }
};

/** Begin union find
 *  Complexity: O(m log n) where m is the number of the operation
 *              and n are the number of objects, in practice is almost O(m)
 **/
int p[mx];

void init_set (int &n) {
  for (int i = 0; i <= n; ++i) p[i] = i;
}

int find_set (int x) {
  return p[x] == x ? p[x] : p[x] = find_set(p[x]);
}

void join (int x, int y) {
  p[find_set(x)] = find_set(y);
}
/**  End union find **/

/** e is a vector with all edges of the graph
 *  return graph of MST
 **/
vector<int> krustal (vector<edge> &e, int &n) {
  init_set(n);

  vector<int> nodes;
  long long ans = 1;
  int size = 0;

  vector<int> visited(n, 0);
  for (int i = 0 ; i < e.size(); ++i) {
    int u = e[i].start;
    int v = e[i].end;
    if (find_set(u) != find_set(v)) {
      join(u, v);
    }
    else {
      nodes.push_back(u);
    }
  }

  return nodes;
}

void dfs (int i, vector<vector<int>> &G, vector<int> &visited, int &s) {
  if (!visited[i]) {
    visited[i] = true;
    s ++;
    for (int j = 0; j < G[i].size(); ++j) {
      dfs(G[i][j], G, visited, s);
    }
  }
}

int mpow (int b, int p, int m) {
  int mask = 1;
  int pow2 = b % m;
  int r = 1;

  while (mask) {
    if (p & mask) r = (r * pow2) % m;
    pow2 = (pow2 * pow2) % m;
    mask <<= 1;
  }

  return r;
}

int main() { IO
  int n;
  cin >> n;

  vector<edge> G;
  vector<vector<int>> G2(n);
  vector<vector<int>> G3(n);
  for (int i = 0; i < n; ++i) {
    int b;
    cin >> b; b --;

    edge e;
    e.start = i;
    e.end = b;

    G.push_back(e);

    G2[i].push_back(b);

    G3[i].push_back(b);
    G3[b].push_back(i);
  }

  vector<int> v = krustal(G, n);
  int t = v.size();
  vector<long long> size(t, 0);
  vector<long long> total(t, 0);
  for (int i = 0; i < v.size(); ++i) {
    vector<int> visited(n, 0);
    int s = 0;
    dfs(v[i], G2, visited, s);
    size[i] = s;
  }

  for (int i = 0; i < v.size(); ++i) {
    vector<int> visited(n, 0);
    int t = 0;
    dfs(v[i], G3, visited, t);
    total[i] = t;
  }

  long long ans = 1;
  for (int i = 0; i < v.size(); ++i) {
    long long a = mpow(2, size[i], mod) - 2;
    long long b = mpow(2, total[i] - size[i], mod);
    ans *= a * b;
    ans %= mod;
  }

  cout << ans << endl;
  return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define endl '\n'
#define D(x) cout << #x << " = " << (x) << endl;
#define IO ios_base::sync_with_stdio(false); cin.tie(NULL);

const int mx = 200100;
const int mod = 1e9 + 7;

long long mpow (long long b, long long e, long long mod) {
  long long ans = 1;
  while (e) {
    if (e & 1) ans = (ans * b) % mod;
    b = (b * b) % mod;
    e >>= 1;
  }
  return ans;
}

void dfs (int i, vector<vector<int>> &G, vector<int> &visited, int &s) {
  if (!visited[i]) {
    visited[i] = true;
    s ++;

    for (int j = 0; j < G[i].size(); ++j) {
      dfs(G[i][j], G, visited, s);
    }
  }
}

struct edge {
  int start, end, weight;
  edge(){} edge(int start, int end, int weight) : start(start), end(end), weight(weight) {}
  bool operator < (const edge &that) const {
    // Change < by > to find maximum spanning tree
    return weight > that.weight;
  }
};

int p[mx];

void init_set (int &n) {
  for (int i = 0; i <= n; ++i) p[i] = i;
}

int find_set (int x) {
  return p[x] == x ? p[x] : p[x] = find_set(p[x]);
}

void join (int x, int y) {
  p[find_set(x)] = find_set(y);
}

void solve (vector<edge> &e, vector<vector<int>> &G, int &n) {
  init_set(n);

  long long ans = 1;
  int tot = 0;

  vector<int> visited(n, 0);
  for (int i = 0 ; i < e.size(); ++i) {
    int u = e[i].start;
    int v = e[i].end;
    if (find_set(u) != find_set(v)) {
      join(u, v);
    }
    else {
      int size = 0;
      dfs(u, G, visited, size);
      tot += size;
      ans *= ((mpow(2, size, mod) - 2) + mod) % mod;
    }
  }

  ans *= mpow(2, n - tot, mod);
  cout << ans % mod << endl;
}

int main() {
  int n;
  cin >> n;

  vector<edge> G;
  vector<vector<int>> G2(n);
  for (int i = 0; i < n; ++i) {
    int b;
    cin >> b; b --;

    edge e;
    e.start = i;
    e.end = b;

    G.push_back(e);
    G2[i].push_back(b);
  }

  solve(G, G2, n);
  return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define endl '\n'
#define D(x) cout << #x << " = " << (x) << endl;
#define IO ios_base::sync_with_stdio(false); cin.tie(NULL);

const int mx = 200100;
const int mod = 1e9 + 7;

long long mpow (long long b, long long e, long long mod) {
  long long ans = 1;
  while (e) {
    if (e & 1) ans = (ans * b) % mod;
    b = (b * b) % mod;
    e >>= 1;
  }
  return ans;
}

void dfs (int i, vector<vector<int>> &G, vector<int> &visited, int &s) {
  if (!visited[i]) {
    visited[i] = true;
    s ++;

    for (int j = 0; j < G[i].size(); ++j) {
      dfs(G[i][j], G, visited, s);
    }
  }
}

struct edge {
  int start, end, weight;
  edge(){} edge(int start, int end, int weight) : start(start), end(end), weight(weight) {}
  bool operator < (const edge &that) const {
    // Change < by > to find maximum spanning tree
    return weight > that.weight;
  }
};

int p[mx];

void init_set (int &n) {
  for (int i = 0; i <= n; ++i) p[i] = i;
}

int find_set (int x) {
  return p[x] == x ? p[x] : p[x] = find_set(p[x]);
}

void join (int x, int y) {
  p[find_set(x)] = find_set(y);
}

void solve (vector<edge> &e, vector<vector<int>> &G, int &n) {
  init_set(n);

  long long ans = 1;
  int tot = 0;

  for (int i = 0 ; i < e.size(); ++i) {
    int u = e[i].start;
    int v = e[i].end;
    if (find_set(u) != find_set(v)) {
      join(u, v);
    }
    else {
      vector<int> visited(n, 0);
      int size = 0;
      dfs(u, G, visited, size);
      tot += size;
      ans *= mpow(2, size, mod) - 2;
    }
  }

  ans *= mpow(2, n - tot, mod);
  cout << ans % mod << endl;
}

int main() {
  int n;
  cin >> n;

  vector<edge> G;
  vector<vector<int>> G2(n);
  for (int i = 0; i < n; ++i) {
    int b;
    cin >> b; b --;

    edge e;
    e.start = i;
    e.end = b;

    G.push_back(e);
    G2[i].push_back(b);
  }

  solve(G, G2, n);
  return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define endl '\n'
#define D(x) cout << #x << " = " << (x) << endl;
#define IO ios_base::sync_with_stdio(false); cin.tie(NULL);

const int mx = 200100;
const int mod = 1e9 + 7;

int mpow (int b, int p, int m) {
  int mask = 1;
  int pow2 = b % m;
  int r = 1;

  while (mask) {
    if (p & mask) r = (r * pow2) % m;
    pow2 = (pow2 * pow2) % m;
    mask <<= 1;
  }

  return r;
}

void dfs (int i, vector<vector<int>> &G, vector<int> &visited, int &s) {
  if (!visited[i]) {
    visited[i] = true;
    s ++;

    for (int j = 0; j < G[i].size(); ++j) {
      dfs(G[i][j], G, visited, s);
    }
  }
}

struct edge {
  int start, end, weight;
  edge(){} edge(int start, int end, int weight) : start(start), end(end), weight(weight) {}
  bool operator < (const edge &that) const {
    // Change < by > to find maximum spanning tree
    return weight > that.weight;
  }
};

int p[mx];

void init_set (int &n) {
  for (int i = 0; i <= n; ++i) p[i] = i;
}

int find_set (int x) {
  return p[x] == x ? p[x] : p[x] = find_set(p[x]);
}

void join (int x, int y) {
  p[find_set(x)] = find_set(y);
}

void solve (vector<edge> &e, vector<vector<int>> &G, int &n) {
  init_set(n);

  long long ans = 1;
  int tot = 0;

  vector<int> visited(n, 0);
  for (int i = 0 ; i < e.size(); ++i) {
    int u = e[i].start;
    int v = e[i].end;
    if (find_set(u) != find_set(v)) {
      join(u, v);
    }
    else {
      int size = 0;
      dfs(u, G, visited, size);
      tot += size;
      ans *= mpow(2, size, mod) - 2;
    }
  }

  ans *= mpow(2, n - tot, mod);
  cout << ans << endl;
}

int main() { IO
  int n;
  cin >> n;

  vector<edge> G;
  vector<vector<int>> G2(n);
  for (int i = 0; i < n; ++i) {
    int b;
    cin >> b; b --;

    edge e;
    e.start = i;
    e.end = b;

    G.push_back(e);
    G2[i].push_back(b);
  }

  solve(G, G2, n);
  return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define endl '\n'
#define D(x) cout << #x << " = " << (x) << endl;
#define IO ios_base::sync_with_stdio(false); cin.tie(NULL);

const int mx = 200100;
const int mod = 1e9 + 7;

long long mpow (long long b, long long e, long long mod) {
  long long ans = 1;
  while (e) {
    if (e & 1) ans = (ans * b) % mod;
    b = (b * b) % mod;
    e >>= 1;
  }
  return ans;
}

void dfs (int i, vector<vector<int>> &G, vector<int> &visited, int &s) {
  if (!visited[i]) {
    visited[i] = true;
    s ++;

    for (int j = 0; j < G[i].size(); ++j) {
      dfs(G[i][j], G, visited, s);
    }
  }
}

struct edge {
  int start, end, weight;
  edge(){} edge(int start, int end, int weight) : start(start), end(end), weight(weight) {}
  bool operator < (const edge &that) const {
    // Change < by > to find maximum spanning tree
    return weight > that.weight;
  }
};

int p[mx];

void init_set (int &n) {
  for (int i = 0; i <= n; ++i) p[i] = i;
}

int find_set (int x) {
  return p[x] == x ? p[x] : p[x] = find_set(p[x]);
}

void join (int x, int y) {
  p[find_set(x)] = find_set(y);
}

void solve (vector<edge> &e, vector<vector<int>> &G, int &n) {
  init_set(n);

  long long ans = 1;
  int tot = 0;

  vector<int> visited(n, 0);
  for (int i = 0 ; i < e.size(); ++i) {
    int u = e[i].start;
    int v = e[i].end;
    if (find_set(u) != find_set(v)) {
      join(u, v);
    }
    else {
      int size = 0;
      dfs(u, G, visited, size);
      long long t = (mpow(2, size, mod) - 2 + mod) % mod;
      ans = (ans * t) % mod;
    }
  }

  int others = 0;
  for (int i = 0; i < n; ++i) others += !visited[i];

  ans = (mpow(2, others, mod) * ans) % mod;
  cout << ans << endl;
}

int main() {
  int n;
  cin >> n;

  vector<edge> G;
  vector<vector<int>> G2(n);
  for (int i = 0; i < n; ++i) {
    int b;
    cin >> b; b --;

    edge e;
    e.start = i;
    e.end = b;

    G.push_back(e);
    G2[i].push_back(b);
  }

  solve(G, G2, n);
  return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define endl '\n'
#define D(x) cout << #x << " = " << (x) << endl;
#define IO ios_base::sync_with_stdio(false); cin.tie(NULL);

/** Krustal implementation
 *  Complexity: O(E log V)
 **/

const int mx = 200100;
const int mod = 1e9 + 7;

struct edge {
  int start, end, weight;
  edge(){} edge(int start, int end, int weight) : start(start), end(end), weight(weight) {}
  bool operator < (const edge &that) const {
    // Change < by > to find maximum spanning tree
    return weight > that.weight;
  }
};

/** Begin union find
 *  Complexity: O(m log n) where m is the number of the operation
 *              and n are the number of objects, in practice is almost O(m)
 **/
int p[mx];

void init_set (int &n) {
  for (int i = 0; i <= n; ++i) p[i] = i;
}

int find_set (int x) {
  return p[x] == x ? p[x] : p[x] = find_set(p[x]);
}

void join (int x, int y) {
  p[find_set(x)] = find_set(y);
}
/**  End union find **/

/** e is a vector with all edges of the graph
 *  return graph of MST
 **/
vector<int> krustal (vector<edge> &e, int &n) {
  init_set(n);

  vector<int> nodes;
  for (int i = 0 ; i < e.size(); ++i) {
    int u = e[i].start;
    int v = e[i].end;
    if (find_set(u) != find_set(v)) {
      join(u, v);
    }
    else {
      nodes.push_back(u);
    }
  }

  return nodes;
}

void dfs (int i, vector<vector<int>> &G, vector<int> &visited, int &s) {
  if (!visited[i]) {
    visited[i] = true;
    s ++;
    for (int j = 0; j < G[i].size(); ++j) {
      dfs(G[i][j], G, visited, s);
    }
  }
}

int mpow (int b, int p, int m) {
  int mask = 1;
  int pow2 = b % m;
  int r = 1;

  while (mask) {
    if (p & mask) r = (r * pow2) % m;
    pow2 = (pow2 * pow2) % m;
    mask <<= 1;
  }

  return r;
}

int main() { IO
  int n;
  cin >> n;

  vector<edge> G;
  vector<vector<int>> G2(n);
  vector<vector<int>> G3(n);
  for (int i = 0; i < n; ++i) {
    int b;
    cin >> b; b --;

    edge e;
    e.start = i;
    e.end = b;

    G.push_back(e);

    G2[i].push_back(b);
  }

  vector<int> v = krustal(G, n);
  int t = v.size();
  vector<long long> size(t, 0);
  for (int i = 0; i < v.size(); ++i) {
    vector<int> visited(n, 0);
    int s = 0;
    dfs(v[i], G2, visited, s);
    size[i] = s;
  }

  long long ans = 1;
  for (int i = 0; i < v.size(); ++i) {
    long long a = mpow(2, size[i], mod) - 2;
    ans *= a;
    ans %= mod;
  }

  int cnt = n - accumulate(size.begin(), size.end(), 0);
  int tot = mpow(2, cnt, mod);
  ans *= tot;
  ans %= mod;

  cout << ans << endl;
  return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define endl '\n'
#define D(x) cout << #x << " = " << (x) << endl;
#define IO ios_base::sync_with_stdio(false); cin.tie(NULL);

/** Krustal implementation
 *  Complexity: O(E log V)
 **/

const int mx = 200100;
const int mod = 1e9 + 7;

struct edge {
  int start, end, weight;
  edge(){} edge(int start, int end, int weight) : start(start), end(end), weight(weight) {}
  bool operator < (const edge &that) const {
    // Change < by > to find maximum spanning tree
    return weight > that.weight;
  }
};

/** Begin union find
 *  Complexity: O(m log n) where m is the number of the operation
 *              and n are the number of objects, in practice is almost O(m)
 **/
int p[mx];

void init_set (int &n) {
  for (int i = 0; i <= n; ++i) p[i] = i;
}

int find_set (int x) {
  return p[x] == x ? p[x] : p[x] = find_set(p[x]);
}

void join (int x, int y) {
  p[find_set(x)] = find_set(y);
}
/**  End union find **/

/** e is a vector with all edges of the graph
 *  return graph of MST
 **/
vector<int> krustal (vector<edge> &e, int &n) {
  init_set(n);

  vector<int> nodes;
  long long ans = 1;
  int size = 0;

  vector<int> visited(n, 0);
  for (int i = 0 ; i < e.size(); ++i) {
    int u = e[i].start;
    int v = e[i].end;
    if (find_set(u) != find_set(v)) {
      join(u, v);
    }
    else {
      nodes.push_back(u);
    }
  }

  return nodes;
}

void dfs (int i, vector<vector<int>> &G, vector<int> &visited, int &s) {
  if (!visited[i]) {
    visited[i] = true;
    s ++;
    for (int j = 0; j < G[i].size(); ++j) {
      dfs(G[i][j], G, visited, s);
    }
  }
}

int mpow (int b, int p, int m) {
  int mask = 1;
  int pow2 = b % m;
  int r = 1;

  while (mask) {
    if (p & mask) r = (r * pow2) % m;
    pow2 = (pow2 * pow2) % m;
    mask <<= 1;
  }

  return r;
}

int main() { IO
  int n;
  cin >> n;

  vector<edge> G;
  vector<vector<int>> G2(n);
  vector<vector<int>> G3(n);
  for (int i = 0; i < n; ++i) {
    int b;
    cin >> b; b --;

    edge e;
    e.start = i;
    e.end = b;

    G.push_back(e);

    G2[i].push_back(b);

    G3[i].push_back(b);
    G3[b].push_back(i);
  }

  vector<int> v = krustal(G, n);
  int t = v.size();
  vector<long long> size(t, 0);
  vector<long long> total(t, 0);
  for (int i = 0; i < v.size(); ++i) {
    vector<int> visited(n, 0);
    int s = 0;
    dfs(v[i], G2, visited, s);
    size[i] = s;
  }

  for (int i = 0; i < v.size(); ++i) {
    vector<int> visited(n, 0);
    int t = 0;
    dfs(v[i], G3, visited, t);
    total[i] = t;
  }

  long long ans = 0;
  for (int i = 0; i < v.size(); ++i) {
    long long a = mpow(2, size[i], mod) - 2;
    long long b = mpow(2, total[i] - size[i], mod);
    ans += a * b;
    ans %= mod;
  }

  cout << ans << endl;
  return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define endl '\n'
#define D(x) cout << #x << " = " << (x) << endl;
#define IO ios_base::sync_with_stdio(false); cin.tie(NULL);

const int mx = 200100;
const int mod = 1e9 + 7;

long long mpow (long long b, long long e, long long mod) {
  long long ans = 1;
  while (e) {
    if (e & 1) ans = (ans * b) % mod;
    b = (b * b) % mod;
    e >>= 1;
  }
  return ans;
}

void dfs (int i, vector<vector<int>> &G, vector<int> &visited, int &s) {
  if (!visited[i]) {
    visited[i] = true;
    s ++;

    for (int j = 0; j < G[i].size(); ++j) {
      dfs(G[i][j], G, visited, s);
    }
  }
}

struct edge {
  int start, end, weight;
  edge(){} edge(int start, int end, int weight) : start(start), end(end), weight(weight) {}
  bool operator < (const edge &that) const {
    // Change < by > to find maximum spanning tree
    return weight > that.weight;
  }
};

int p[mx];

void init_set (int &n) {
  for (int i = 0; i <= n; ++i) p[i] = i;
}

int find_set (int x) {
  return p[x] == x ? p[x] : p[x] = find_set(p[x]);
}

void join (int x, int y) {
  p[find_set(x)] = find_set(y);
}

void solve (vector<edge> &e, vector<vector<int>> &G, int &n) {
  init_set(n);

  long long ans = 1;
  int tot = 0;

  vector<int> visited(n, 0);
  for (int i = 0 ; i < e.size(); ++i) {
    int u = e[i].start;
    int v = e[i].end;
    if (find_set(u) != find_set(v)) {
      join(u, v);
    }
    else {
      int size = 0;
      dfs(u, G, visited, size);
      ans *= ((mpow(2, size, mod) - 2) + mod) % mod;
    }
  }

  int others = 0;
  for (int i = 0; i < n; ++i) others += !visited[i];

  ans *= mpow(2, others, mod);
  cout << ans % mod << endl;
}

int main() {
  int n;
  cin >> n;

  vector<edge> G;
  vector<vector<int>> G2(n);
  for (int i = 0; i < n; ++i) {
    int b;
    cin >> b; b --;

    edge e;
    e.start = i;
    e.end = b;

    G.push_back(e);
    G2[i].push_back(b);
  }

  solve(G, G2, n);
  return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define endl '\n'
#define D(x) cout << #x << " = " << (x) << endl;
#define IO ios_base::sync_with_stdio(false); cin.tie(NULL);

int mpow (int b, int p, int m) {
  int mask = 1;
  int pow2 = b % m;
  int r = 1;

  while (mask) {
    if (p & mask) r = (r * pow2) % m;
    pow2 = (pow2 * pow2) % m;
    mask <<= 1;
  }

  return r;
}

void dfs (int i, vector<vector<int>> &G, vector<int> &visited, vector<int> &visited2, int &s) {
  if (!visited[i]) {
    visited[i] = true;
    if (!visited2[i]) s ++;
    for (int j = 0; j < G[i].size(); ++j) {
      dfs(G[i][j], G, visited, visited2, s);
    }
  }
}

/** Krustal implementation
 *  Complexity: O(E log V)
 **/

const int mx = 200100;
const int mod = 1e9 + 7;

struct edge {
  int start, end, weight;
  edge(){} edge(int start, int end, int weight) : start(start), end(end), weight(weight) {}
  bool operator < (const edge &that) const {
    // Change < by > to find maximum spanning tree
    return weight > that.weight;
  }
};

/** Begin union find
 *  Complexity: O(m log n) where m is the number of the operation
 *              and n are the number of objects, in practice is almost O(m)
 **/
int p[mx];

void init_set (int &n) {
  for (int i = 0; i <= n; ++i) p[i] = i;
}

int find_set (int x) {
  return p[x] == x ? p[x] : p[x] = find_set(p[x]);
}

void join (int x, int y) {
  p[find_set(x)] = find_set(y);
}
/**  End union find **/

/** e is a vector with all edges of the graph
 *  return graph of MST
 **/
void krustal (vector<edge> &e, vector<vector<int>> &G, int &n) {
  init_set(n);

  long long ans = 1;
  for (int i = 0 ; i < e.size(); ++i) {
    int u = e[i].start;
    int v = e[i].end;
    if (find_set(u) != find_set(v)) {
      join(u, v);
    }
    else {
      vector<int> visited(n, 0);
      int size = 0;
      int cur = u;
      while (true) {
        if (!visited[cur]) {
          size ++;
          visited[cur] = true;
          cur = G[cur][0];
        }
        else break;
      }

      vector<int> visited2(n, 0);
      int r = 0;
      dfs(u, G, visited2, visited, r);
      ans *= mpow(2, size, mod) - 2;
      ans *= mpow(2, r, mod);
    }
  }

  cout << ans << endl;
}

int main() {
  int n;
  cin >> n;

  vector<edge> G;
  vector<vector<int>> G2(n);
  for (int i = 0; i < n; ++i) {
    int b;
    cin >> b; b --;

    edge e;
    e.start = i;
    e.end = b;

    G.push_back(e);

    G2[i].push_back(b);
    G2[b].push_back(i);
  }

  krustal(G, G2, n);
  return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define endl '\n'
#define D(x) cout << #x << " = " << (x) << endl;
#define IO ios_base::sync_with_stdio(false); cin.tie(NULL);

int mpow (int b, int p, int m) {
  int mask = 1;
  int pow2 = b % m;
  int r = 1;

  while (mask) {
    if (p & mask) r = (r * pow2) % m;
    pow2 = (pow2 * pow2) % m;
    mask <<= 1;
  }

  return r;
}

void dfs (int i, vector<vector<int>> &G, vector<int> &visited, vector<int> &visited2, int &s) {
  if (!visited[i]) {
    visited[i] = true;
    if (!visited2[i]) s ++;
    for (int j = 0; j < G[i].size(); ++j) {
      dfs(G[i][j], G, visited, visited2, s);
    }
  }
}

/** Krustal implementation
 *  Complexity: O(E log V)
 **/

const int mx = 200100;
const int mod = 1e9 + 7;

struct edge {
  int start, end, weight;
  edge(){} edge(int start, int end, int weight) : start(start), end(end), weight(weight) {}
  bool operator < (const edge &that) const {
    // Change < by > to find maximum spanning tree
    return weight > that.weight;
  }
};

/** Begin union find
 *  Complexity: O(m log n) where m is the number of the operation
 *              and n are the number of objects, in practice is almost O(m)
 **/
int p[mx];

void init_set (int &n) {
  for (int i = 0; i <= n; ++i) p[i] = i;
}

int find_set (int x) {
  return p[x] == x ? p[x] : p[x] = find_set(p[x]);
}

void join (int x, int y) {
  p[find_set(x)] = find_set(y);
}
/**  End union find **/

/** e is a vector with all edges of the graph
 *  return graph of MST
 **/
void krustal (vector<edge> &e, vector<vector<int>> &G, int &n) {
  init_set(n);

  long long ans = 1;
  for (int i = 0 ; i < e.size(); ++i) {
    int u = e[i].start;
    int v = e[i].end;
    if (find_set(u) != find_set(v)) {
      join(u, v);
    }
    else {
      vector<int> visited(n, 0);
      int size = 0;
      int cur = u;
      while (true) {
        if (!visited[cur]) {
          size ++;
          visited[cur] = true;
          cur = G[cur][0];
        }
        else break;
      }

      vector<int> visited2(n, 0);
      int r = 0;
      dfs(u, G, visited2, visited, r);
      ans *= mpow(2, size, mod) - 2;
      ans *= mpow(2, r, mod);
    }
  }

  cout << ans << endl;
}

int main() { IO
  int n;
  cin >> n;

  vector<edge> G;
  vector<vector<int>> G2(n);
  for (int i = 0; i < n; ++i) {
    int b;
    cin >> b; b --;

    edge e;
    e.start = i;
    e.end = b;

    G.push_back(e);

    G2[i].push_back(b);
    G2[b].push_back(i);
  }

  krustal(G, G2, n);
  return 0;
}


#include<iostream>
#include<cstdlib>
#include "cmath"
#include<string>
#include<vector>
#include<sstream>
#include<unordered_map>

const long long mod = 1e9 + 7;
const int MAX = 2 * 1e5 + 1;
std::vector<int> cycles;
int visited[MAX];
int cnt = 0;
int a[MAX];

void dfsc(int v) {
    cycles[cnt]++;
   // std::cout << v << " cyc\n";
    visited[v] = 3;
   //std::cout << a[v] << " " <<visited[a[v]] << '\n';
    if (visited[a[v]] == 3)
        return;
    dfsc(a[v]);
}

void dfs(int v) {
    visited[v] = 2;

    if (visited[a[v]] == 0) {
        dfs(a[v]);
    }
    else if (visited[a[v]] == 1) {
        visited[v] = 1;
        return;
    }

    else {
        cycles.push_back(0);
      //  std::cout << v << " a\n";
        dfsc(v);
        cnt++;
    }
    visited[v] = 1;
}

int main() {
    int n;
  //  freopen("file.in", "r", stdin);

    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cin >> n;

   ;
    for (int i = 1; i <= n; ++i)
        std::cin >> a[i];

    long long steps[n + 1];
    steps[0] = 1;
    steps[1] = 2;
    for (int i = 2; i <= n; ++i)
        steps[i] = (steps[i - 1] * 2LL) % mod;

    for (int i = 1; i <= n; ++i)
        if (visited[i] == 0)
            dfs(i);

    int compcnt = n;
    long long ans = 1;
    for (int i = 0; i < cycles.size(); ++i) {
        ans = (ans * (steps[cycles[i]] - 2)) % mod;
        compcnt -= cycles[i];
    }
    ans = (ans * steps[compcnt]) % mod;
   // std::cout << cnt <<  " ? " << cycles.size() << '\n';
    ans = ans % mod;
    if (ans < 0)
        ans += mod;
    int ansf = ans;
    std::cout << ans;
}


#include<iostream>
#include<cstdlib>
#include "cmath"
#include<string>
#include<vector>
#include<sstream>
#include<unordered_map>

const long long mod = 10e9 + 7;
const int MAX = 2 * 10e5 + 1;
std::vector<int> cycles;
int visited[MAX];
int cnt = 0;
int a[MAX];

void dfsc(int v) {
    cycles[cnt]++;
   // std::cout << v << " cyc\n";
    visited[v] = 3;
   //std::cout << a[v] << " " <<visited[a[v]] << '\n';
    if (visited[a[v]] == 3)
        return;
    dfsc(a[v]);
}

void dfs(int v) {
    visited[v] = 2;

    if (visited[a[v]] == 0) {
        dfs(a[v]);
    }
    else if (visited[a[v]] == 1) {
        visited[v] = 1;
        return;
    }

    else {
        cycles.push_back(0);
      //  std::cout << v << " a\n";
        dfsc(v);
        cnt++;
    }
    visited[v] = 1;
}

int main() {
    int n;
  //  freopen("file.in", "r", stdin);

    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cin >> n;

   ;
    for (int i = 1; i <= n; ++i)
        std::cin >> a[i];

    long long steps[n + 1];
    steps[0] = 1;
    steps[1] = 2;
    for (int i = 2; i <= n; ++i)
        steps[i] = (steps[i - 1] * 2 + mod) % mod;

    for (int i = 1; i <= n; ++i)
        if (visited[i] == 0)
            dfs(i);

    int compcnt = n;
    long long ans = 1;
    for (int i = 0; i < cycles.size(); ++i) {
        ans = (ans * (steps[cycles[i]] - 2)) % mod;
        compcnt -= cycles[i];
    }
    ans = (ans * steps[compcnt]) % mod;
   // std::cout << cnt <<  " ? " << cycles.size() << '\n';
    if (ans < 0)
        ans += mod;
    std::cout << ans;
}


#include<iostream>
#include<cstdlib>
#include "cmath"
#include<string>
#include<vector>
#include<sstream>
#include<unordered_map>

const long long mod = 10e9 + 7;
const int MAX = 2 * 10e5 + 1;
std::vector<int> cycles;
int visited[MAX];
int cnt = 0;
int a[MAX];

void dfsc(int v) {
    cycles[cnt]++;
   // std::cout << v << " cyc\n";
    visited[v] = 3;
   //std::cout << a[v] << " " <<visited[a[v]] << '\n';
    if (visited[a[v]] == 3)
        return;
    dfsc(a[v]);
}

void dfs(int v) {
    visited[v] = 2;

    if (visited[a[v]] == 0) {
        dfs(a[v]);
    }
    else if (visited[a[v]] == 1) {
        visited[v] = 1;
        return;
    }

    else {
        cycles.push_back(0);
      //  std::cout << v << " a\n";
        dfsc(v);
        cnt++;
    }
    visited[v] = 1;
}

int main() {
    int n;
  //  freopen("file.in", "r", stdin);

    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cin >> n;

   ;
    for (int i = 1; i <= n; ++i)
        std::cin >> a[i];

    long long steps[n + 1];
    steps[0] = 1;
    steps[1] = 2;
    for (int i = 2; i <= n; ++i)
        steps[i] = (steps[i - 1] * 2LL) % mod;

    for (int i = 1; i <= n; ++i)
        if (visited[i] == 0)
            dfs(i);

    int compcnt = n;
    long long ans = 1;
    for (int i = 0; i < cycles.size(); ++i) {
        ans = (ans * (steps[cycles[i]] - 2)) % mod;
        compcnt -= cycles[i];
    }
    ans = (ans * steps[compcnt]) % mod;
   // std::cout << cnt <<  " ? " << cycles.size() << '\n';
    ans = ans % mod;
    if (ans < 0)
        ans += mod;
    int ansf = ans;
    std::cout << ansf;
}


#include<iostream>
#include<cstdlib>
#include "cmath"
#include<string>
#include<vector>
#include<sstream>
#include<unordered_map>

const long long mod = 10e9 + 7;
const int MAX = 2 * 10e5 + 1;
std::vector<int> cycles;
int visited[MAX];
int cnt = 0;
int a[MAX];

void dfsc(int v) {
    cycles[cnt]++;
    visited[v] = 3;
    if (visited[a[v]] == 3)
        return;
    dfsc(a[v]);
}

void dfs(int v) {
    visited[v] = 2;
    if (visited[a[v]] == 0) {
        dfs(a[v]);
    }
    else if (visited[a[v]] == 1) {
        visited[v] = 1;
        return;
    }

    else {
        cycles.push_back(0);
        dfsc(v);
        cnt++;
    }
    visited[v] = 1;
}

int main() {
    int n;
   // freopen("file.in", "r", stdin);

    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cin >> n;

   ;
    for (int i = 1; i <= n; ++i)
        std::cin >> a[i];

    long long steps[n + 1];
    steps[0] = 1;
    steps[1] = 2;
    for (int i = 2; i <= n; ++i)
        steps[i] = (steps[i - 1] * 2 + mod) % mod;

    for (int i = 1; i <= n; ++i)
        if (visited[i] == 0)
            dfs(i);

    int cnt = n;
    long long ans = 0;
    for (int i = 0; i < cycles.size(); ++i) {
        if (ans == 0)
            ans = (steps[cycles[i]] - 2) % mod;
        else
            ans = (ans * (steps[cycles[i]] - 2)) % mod;
        cnt -= cycles[i];
    }
    ans = (ans * steps[cnt]) % mod;
    ans = ans % mod;
    if (ans < 0)
        ans += mod;

    std::cout << ans;
}


#include<iostream>
#include<cstdlib>
#include "cmath"
#include<string>
#include<vector>
#include<sstream>
#include<unordered_map>

const long long mod = 10e9 + 7;
const int MAX = 2 * 10e5 + 1;
std::vector<int> cycles;
int visited[MAX];
int cnt = 0;
int a[MAX];

void dfsc(int v) {
    cycles[cnt]++;
   // std::cout << v << " cyc\n";
    visited[v] = 3;
   //std::cout << a[v] << " " <<visited[a[v]] << '\n';
    if (visited[a[v]] == 3)
        return;
    dfsc(a[v]);
}

void dfs(int v) {
    visited[v] = 2;

    if (visited[a[v]] == 0) {
        dfs(a[v]);
    }
    else if (visited[a[v]] == 1) {
        visited[v] = 1;
        return;
    }

    else {
        cycles.push_back(0);
      //  std::cout << v << " a\n";
        dfsc(v);
        cnt++;
    }
    visited[v] = 1;
}

int main() {
    int n;
  //  freopen("file.in", "r", stdin);

    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cin >> n;

   ;
    for (int i = 1; i <= n; ++i)
        std::cin >> a[i];

    long long steps[n + 1];
    steps[0] = 1;
    steps[1] = 2;
    for (int i = 2; i <= n; ++i)
        steps[i] = (steps[i - 1] * 2LL) % mod;

    for (int i = 1; i <= n; ++i)
        if (visited[i] == 0)
            dfs(i);

    int compcnt = n;
    long long ans = 1;
    for (int i = 0; i < cycles.size(); ++i) {
        ans = (ans * (steps[cycles[i]] - 2)) % mod;
        compcnt -= cycles[i];
    }
    ans = (ans * steps[compcnt]) % mod;
   // std::cout << cnt <<  " ? " << cycles.size() << '\n';
    if (ans < 0)
        ans += mod;
    std::cout << ans;
}


#include<iostream>
#include<cstdlib>
#include "cmath"
#include<string>
#include<vector>
#include<sstream>
#include<unordered_map>

const long long mod = 10e9 + 7;
const int MAX = 2 * 10e5 + 1;
std::vector<int> cycles;
int visited[MAX];
int cnt = 0;
int a[MAX];

void dfsc(int v) {
    cycles[cnt]++;
   // std::cout << v << " cyc\n";
    visited[v] = 3;
   //std::cout << a[v] << " " <<visited[a[v]] << '\n';
    if (visited[a[v]] == 3)
        return;
    dfsc(a[v]);
}

void dfs(int v) {
    visited[v] = 2;

    if (visited[a[v]] == 0) {
        dfs(a[v]);
    }
    else if (visited[a[v]] == 1) {
        visited[v] = 1;
        return;
    }

    else {
        cycles.push_back(0);
      //  std::cout << v << " a\n";
        dfsc(v);
        cnt++;
    }
    visited[v] = 1;
}

int main() {
    int n;
  //  freopen("file.in", "r", stdin);

    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cin >> n;

   ;
    for (int i = 1; i <= n; ++i)
        std::cin >> a[i];

    long long steps[n + 1];
    steps[0] = 1;
    steps[1] = 2;
    for (int i = 2; i <= n; ++i)
        steps[i] = (steps[i - 1] * 2 + mod) % mod;

    for (int i = 1; i <= n; ++i)
        if (visited[i] == 0)
            dfs(i);

    int compcnt = n;
    long long ans = 0;
    for (int i = 0; i < cycles.size(); ++i) {
        if (ans == 0)
            ans = (steps[cycles[i]] - 2) % mod;
        else
            ans = (ans * (steps[cycles[i]] - 2)) % mod;
        compcnt -= cycles[i];
    }
    ans = (ans * steps[compcnt]) % mod;
    ans = ans % mod;
   // std::cout << cnt <<  " ? " << cycles.size() << '\n';
    if (ans < 0)
        ans += mod;
    std::cout << ans;
}


#include<iostream>
#include<cstdlib>
#include "cmath"
#include<string>
#include<vector>
#include<sstream>
#include<unordered_map>

const long long mod = 10e9 + 7;
const int MAX = 2 * 10e5 + 1;
std::vector<int> cycles;
int visited[MAX];
int cnt = 0;
int a[MAX];

void dfsc(int v) {
    cycles[cnt]++;
   // std::cout << v << " cyc\n";
    visited[v] = 3;
   //std::cout << a[v] << " " <<visited[a[v]] << '\n';
    if (visited[a[v]] == 3)
        return;
    dfsc(a[v]);
}

void dfs(int v) {
    visited[v] = 2;

    if (visited[a[v]] == 0) {
        dfs(a[v]);
    }
    else if (visited[a[v]] == 1) {
        visited[v] = 1;
        return;
    }

    else {
        cycles.push_back(0);
      //  std::cout << v << " a\n";
        dfsc(v);
        cnt++;
    }
    visited[v] = 1;
}

int main() {
    int n;
  //  freopen("file.in", "r", stdin);

    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cin >> n;

   ;
    for (int i = 1; i <= n; ++i)
        std::cin >> a[i];

    long long steps[n + 1];
    steps[0] = 1;
    steps[1] = 2;
    for (int i = 2; i <= n; ++i)
        steps[i] = (steps[i - 1] * 2LL) % mod;

    for (int i = 1; i <= n; ++i)
        if (visited[i] == 0)
            dfs(i);

    int compcnt = n;
    long long ans = 1;
    for (int i = 0; i < cycles.size(); ++i) {
        ans = (ans * (steps[cycles[i]] - 2)) % mod;
        compcnt -= cycles[i];
    }
    ans = (ans * steps[compcnt]) % mod;
   // std::cout << cnt <<  " ? " << cycles.size() << '\n';
    ans = ans % mod;
    if (ans < 0)
        ans += mod;
    int ansf = ans;
    std::cout << ans;
}


#include<iostream>
#include<cstdlib>
#include "cmath"
#include<string>
#include<vector>
#include<sstream>
#include<unordered_map>

const long long mod = 10e9 + 7;
const int MAX = 2 * 10e5 + 1;
std::vector<int> cycles;
int visited[MAX];
int cnt = 0;
int a[MAX];

void dfsc(int v) {
    cycles[cnt]++;
   // std::cout << v << " cyc\n";
    visited[v] = 3;
   //std::cout << a[v] << " " <<visited[a[v]] << '\n';
    if (visited[a[v]] == 3)
        return;
    dfsc(a[v]);
}

void dfs(int v) {
    visited[v] = 2;

    if (visited[a[v]] == 0) {
        dfs(a[v]);
    }
    else if (visited[a[v]] == 1) {
        visited[v] = 1;
        return;
    }

    else {
        cycles.push_back(0);
      //  std::cout << v << " a\n";
        dfsc(v);
        cnt++;
    }
    visited[v] = 1;
}

int main() {
    int n;
  //  freopen("file.in", "r", stdin);

    std::ios_base::sync_with_stdio(false);
    std::cin.tie(NULL);
    std::cin >> n;

   ;
    for (int i = 1; i <= n; ++i)
        std::cin >> a[i];

    long long steps[n + 1];
    steps[0] = 1;
    steps[1] = 2;
    for (int i = 2; i <= n; ++i)
        steps[i] = (steps[i - 1] * 2LL) % mod;

    for (int i = 1; i <= n; ++i)
        if (visited[i] == 0)
            dfs(i);

    int compcnt = n;
    long long ans = 1;
    for (int i = 0; i < cycles.size(); ++i) {
        ans = (ans * (steps[cycles[i]] - 2)) % mod;
        compcnt -= cycles[i];
    }
    ans = (ans * steps[compcnt]) % mod;
   // std::cout << cnt <<  " ? " << cycles.size() << '\n';
    int ansf = ans;
    if (ansf < 0)
        ansf += mod;

    std::cout << ansf;
}