#include <bits/stdc++.h>
#include <cmath>
#include <string>
#include <sstream>
#include <vector>
#include <stdio.h>
#define ll              long long
#define fr(i,j,p)       for(ll i=j; i<p ;i++)
#define frr(i,j,p)      for(ll i=j; i>=p ;i--)
#define PI              3.14159265358979323846
#define print(m)        std::cout << std::fixed << std::setprecision(11) << m <<endl;
#define mkp             make_pair
#define pb              push_back
#define M1              32
#define lbindex(a,x)    (int)(lower_bound(a.begin(), a.end(),x)-a.begin());
#define upindex(a,x)    (int)(upper_bound(a.begin(), a.end(),x)-a.begin());
#define all(a)          (a).begin(),(a).end()
#define mem(b,a,n)      memset(b,a,sizeof(b[0])*n);
#define meme(b,a,n,m)   memset(b,a,sizeof(b[0][0])*n*m);
#define ff              first
#define ss              second
#define endl            '\n'

using namespace std;
typedef vector<ll> vl;		typedef vector<int> vi;
typedef vector<vl> vvl;	  	typedef vector<vi> vvi;
typedef pair<ll, ll> pll;	typedef pair<int, int> pii;

ll mdd=998244353;
ll md=1000000007;
vi v[1000006];
int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    ll n,m;   cin>>n>>m;
    fr(i,0,n)
    {
        ll a;
        cin>>a;
        v[a].pb(i+1);
    }
    fr(i,0,m)
    {
        ll l,r,x;   cin>>l>>r>>x;
        int st=lbindex(v[x],l);
        int lt=lbindex(v[x],r);
        if(lt==v[x].size() || v[x][lt]!=r)   {cout<<r<<endl;}
        else if(v[x][st]!=l)   {cout<<l<<endl;}
        else if(lt-st==r-l)    {cout<<-1<<endl;}
        else
        {
            int start=l,last=r;
            while(start<last)
            {
                int mid=(l+r)/2;
                int z=lbindex(v[x],mid);
                if(v[x][z]!=mid)  {cout<<mid<<endl; break;}
                if(z-st==mid-l) {start=mid;}
                else last=mid;
            }
        }
    }

    return 0;
}


#include <bits/stdc++.h>
#include <cmath>
#include <string>
#include <sstream>
#include <vector>
#include <stdio.h>
#define ll              long long
#define fr(i,j,p)       for(ll i=j; i<p ;i++)
#define frr(i,j,p)      for(ll i=j; i>=p ;i--)
#define PI              3.14159265358979323846
#define print(m)        std::cout << std::fixed << std::setprecision(11) << m <<endl;
#define mkp             make_pair
#define pb              push_back
#define M1              32
#define lbindex(a,x)    (int)(lower_bound(a.begin(), a.end(),x)-a.begin());
#define upindex(a,x)    (int)(upper_bound(a.begin(), a.end(),x)-a.begin());
#define all(a)          (a).begin(),(a).end()
#define mem(b,a,n)      memset(b,a,sizeof(b[0])*n);
#define meme(b,a,n,m)   memset(b,a,sizeof(b[0][0])*n*m);
#define ff              first
#define ss              second
#define endl            '\n'

using namespace std;
typedef vector<ll> vl;		typedef vector<int> vi;
typedef vector<vl> vvl;	  	typedef vector<vi> vvi;
typedef pair<ll, ll> pll;	typedef pair<int, int> pii;

ll mdd=998244353;
ll md=1000000007;

int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    ll n,m;   cin>>n>>m;
    ll sum[n+1];    sum[0]=0;
    fr(i,1,n+1)
    {
        ll a;   cin>>a;
        sum[i]=sum[i-1]+a;
    }
    fr(i,0,m)
    {
        ll l,r,x;   cin>>l>>r>>x;
        ll z=sum[r]-sum[l-1];
        if(z==(x*(r-l+1)))  {cout<<-1<<endl;}
        else
        {
            ll start=l,last=r;
            while(start<last)
            {
                ll mid=(last+start)/2ll;
                z=sum[mid]-sum[start-1];
                if(z==(x*(mid-start+1)))  start=mid+1;
                else last=mid;
            }
            cout<<start<<endl;
        }
    }

    return 0;
}


#include <bits/stdc++.h>
#include <cmath>
#include <string>
#include <sstream>
#include <vector>
#include <stdio.h>
#define ll              long long
#define fr(i,j,p)       for(ll i=j; i<p ;i++)
#define frr(i,j,p)      for(ll i=j; i>=p ;i--)
#define PI              3.14159265358979323846
#define print(m)        std::cout << std::fixed << std::setprecision(11) << m <<endl;
#define mkp             make_pair
#define pb              push_back
#define M1              32
#define lbindex(a,x)    (int)(lower_bound(a.begin(), a.end(),x)-a.begin());
#define upindex(a,x)    (int)(upper_bound(a.begin(), a.end(),x)-a.begin());
#define all(a)          (a).begin(),(a).end()
#define mem(b,a,n)      memset(b,a,sizeof(b[0])*n);
#define meme(b,a,n,m)   memset(b,a,sizeof(b[0][0])*n*m);
#define ff              first
#define ss              second
#define endl            '\n'

using namespace std;
typedef vector<ll> vl;		typedef vector<int> vi;
typedef vector<vl> vvl;	  	typedef vector<vi> vvi;
typedef pair<ll, ll> pll;	typedef pair<int, int> pii;

ll mdd=998244353;
ll md=1000000007;
int main()
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    ll n,m;   cin>>n>>m;
    ll a[n+1],diff[n+1];
    fr(i,1,n+1)   cin>>a[i];
    frr(i,n,1)
    {
        int j=i-1;
        while(j>0)
        {
            if(a[j]!=a[i])
            {
                int z=j;
                while(j<=i)
                {
                    diff[j]=z;  j++;
                }
                i=z+1; break;
            }
            j--;
        }
        if(j==0)
        {
            while(j<=i)
            {
                diff[j]=-1;  j++;
            }
            break;
        }
    }
    fr(i,0,m)
    {
        ll l,r,x;   cin>>l>>r>>x;
        if(a[r]!=x) {cout<<r<<endl; continue;}
        if(diff[r]==-1 || diff[r]<l)    {cout<<-1<<endl; continue;}
        cout<<diff[r]<<endl;
    }

    return 0;
}


#define ll long long int
#include<bits/stdc++.h>
using namespace std ;

#define mod 1000000007
vector<vector<ll> > g(200001) ;
vector<vector<ll> > g1(200001) ;
vector<bool> vis(200001 , false) ;
stack<ll> s ;
vector<vector<ll> > cycle ;
void dfs(ll node)
{
    vis[node] = true ;

    for(ll i = 0 ; i < g[node].size() ; i++)
    {
        if(!vis[g[node][i]])
            dfs(g[node][i]) ;
    }

    s.push(node) ;
}

ll cnt = 0 ;
void dfs1(ll node)
{
    vis[node] = true ;
    cnt++ ;
    for(ll i = 0 ; i < g1[node].size() ; i++)
    {
        if(!vis[g1[node][i]])
            dfs1(g1[node][i]) ;
    }
}
#define pi pair<pair<int , char>, pair<int , int> >
ll ncr(ll n, ll r, ll p)
{
    // Optimization for the cases when r is large
    if (r > n - r)

        r = n - r;

    // The array C is going to store last row of
    // pascal triangle at the end. And last entry
    // of last row is nCr
    ll C[r + 1];
    memset(C, 0, sizeof(C));

    C[0] = 1; // Top row of Pascal Triangle

    // One by constructs remaining rows of Pascal
    // Triangle from top to bottom
    for (ll i = 1; i <= n; i++) {

        // Fill entries of current row using previous
        // row values
        for (ll j = min(i, r); j > 0; j--)

            // nCj = (n-1)Cj + (n-1)C(j-1);
            C[j] = (C[j] + C[j - 1]) % p;
    }
    return C[r];
}
int main()
{

   ll n ;
   cin>>n ;

   vector<ll> a(n + 1) ;

   for(ll i = 1 ; i <= n ; i++)
   {
       cin>>a[i] ;
       g[i].push_back(a[i]) ;
       g1[a[i]].push_back(i) ;
   }

   for(ll i = 1 ; i <= n ; i++)
   {
       if(!vis[i])
        dfs(i) ;
   }
   for(ll i = 0 ; i <= 200000 ; i++)
       vis[i] = false ;

   ll ans  =1 ;
   while(!s.empty())
   {
       ll x = s.top() ;
       s.pop() ;

       if(!vis[x])
       {
           dfs1(x) ;
           if(cnt == 1)
           {
               ans = (ans*2)%mod ;
           }
           else
           {
               ll sum = 0 ;
               for(ll i = 1 ; i < cnt ; i++)
                sum += ncr(cnt , i , mod) ;

               ans = (ans*sum)%mod ;
           }

       }
   }

   cout<<ans ;

}


#define ll long long int
#include<bits/stdc++.h>
using namespace std ;

#define mod 1000000007
vector<vector<ll> > g(200001) ;
vector<vector<ll> > g1(200001) ;
vector<bool> vis(200001 , false) ;
stack<ll> s ;
vector<vector<ll> > cycle ;
void dfs(ll node)
{
    vis[node] = true ;

    for(ll i = 0 ; i < g[node].size() ; i++)
    {
        if(!vis[g[node][i]])
            dfs(g[node][i]) ;
    }

    s.push(node) ;
}

vector<ll> cc ;
void dfs1(ll node)
{
    vis[node] = true ;
    cc.push_back(node) ;
    for(ll i = 0 ; i < g1[node].size() ; i++)
    {
        if(!vis[g1[node][i]])
            dfs1(g1[node][i]) ;
    }
}
#define pi pair<pair<int , char>, pair<int , int> >
ll ncr(ll n , ll r)
{
    long long p = 1, k = 1;

    if (n - r < r)
        r = n - r;

    if (r != 0) {
        while (r) {
            p *= n;
            k *= r;

            long long m = __gcd(p, k);

            p /= m;
            k /= m;

            n--;
            r--;
        }

    }

    else
        p = 1;

    return p ;

}
int main()
{

   ll n ;
   cin>>n ;

   vector<ll> a(n + 1) ;

   for(ll i = 1 ; i <= n ; i++)
   {
       cin>>a[i] ;
       g[i].push_back(a[i]) ;
       g1[a[i]].push_back(i) ;
   }

   for(ll i = 1 ; i <= n ; i++)
   {
       if(!vis[i])
        dfs(i) ;
   }
   for(ll i = 0 ; i <= 200000 ; i++)
       vis[i] = false ;

   while(!s.empty())
   {
       ll x = s.top() ;
       s.pop() ;

       if(!vis[x])
       {
           dfs1(x) ;
           cycle.push_back(cc) ;
           cc.clear() ;
       }
   }
  ll ans  = 1 ;
   for(ll i = 0 ; i < cycle.size() ; i++)
   {
      if(cycle[i].size() == 1)
      {
          ans *= 2 ;
      }
      else
      {
          ll sum = 0 ;
          for(ll j =  1 ; j < cycle[i].size() ; j++)
          {
              sum += ncr(cycle[i].size() , j ) ;
          }

          ans *= sum%mod ;

          ans = ans%mod ;

      }

   }

   cout<<ans ;

}


#define ll long long int
#include<bits/stdc++.h>
using namespace std ;

#define mod 1000000007
vector<vector<ll> > g(200001) ;
vector<vector<ll> > g1(200001) ;
vector<bool> vis(200001 , false) ;
stack<ll> s ;
vector<vector<ll> > cycle ;
void dfs(ll node)
{
    vis[node] = true ;

    for(ll i = 0 ; i < g[node].size() ; i++)
    {
        if(!vis[g[node][i]])
            dfs(g[node][i]) ;
    }

    s.push(node) ;
}

ll cnt = 0 ;
void dfs1(ll node)
{
    vis[node] = true ;
    cnt++ ;
    for(ll i = 0 ; i < g1[node].size() ; i++)
    {
        if(!vis[g1[node][i]])
            dfs1(g1[node][i]) ;
    }
}
#define pi pair<pair<int , char>, pair<int , int> >
ll ncr(ll n, ll r)
{

    // p holds the value of n*(n-1)*(n-2)...,
    // k holds the value of r*(r-1)...
    ll p = 1, k = 1;

    // C(n, r) == C(n, n-r),
    // choosing the smaller value
    if (n - r < r)
        r = n - r;

    if (r != 0) {
        while (r) {
            p *= n;
            k *= r;

            // gcd of p, k
            ll m = __gcd(p, k);

            // dividing by gcd, to simplify product
            // division by their gcd saves from the overflow
            p /= m;
            k /= m;

            n--;
            r--;
        }

        // k should be simplified to 1
        // as C(n, r) is a natural number
        // (denominator should be 1 ) .
    }

    else
        p = 1;

    // if our approach is correct p = ans and k =1
  return p ;
}
ll binpow(ll x,ll y){
    x = x%mod;
    ll res = 1;
    while(y){
        if(y&1) res = (res*x)%mod;
        x = (x*x)%mod;
        y /= 2;
    }
    return res;
}
int main()
{

   ll n ;
   cin>>n ;

   vector<ll> a(n + 1) ;

   for(ll i = 1 ; i <= n ; i++)
   {
       cin>>a[i] ;
       g[i].push_back(a[i]) ;
       g1[a[i]].push_back(i) ;
   }

   for(ll i = 1 ; i <= n ; i++)
   {
       if(!vis[i])
        dfs(i) ;
   }
   for(ll i = 0 ; i <= 200000 ; i++)
       vis[i] = false ;

   ll ans  =1 ;
   while(!s.empty())
   {
       ll x = s.top() ;
       s.pop() ;

       if(!vis[x])
       {
           dfs1(x) ;
           if(cnt == 1)
           {
               ans = (ans*2)%mod ;
           }
           else
           {

               ll sum = binpow(2 , cnt) - 2 ;
               ans = (ans*(sum + mod))%mod ;
           }
           cnt = 0 ;
       }
   }

   cout<<ans ;

}


#define ll long long int
#include<bits/stdc++.h>
using namespace std ;

#define mod 1000000007
vector<vector<ll> > g(200001) ;
vector<vector<ll> > g1(200001) ;
vector<bool> vis(200001 , false) ;
stack<ll> s ;
vector<vector<ll> > cycle ;
void dfs(ll node)
{
    vis[node] = true ;

    for(ll i = 0 ; i < g[node].size() ; i++)
    {
        if(!vis[g[node][i]])
            dfs(g[node][i]) ;
    }

    s.push(node) ;
}

vector<ll> cc ;
void dfs1(ll node)
{
    vis[node] = true ;
    cc.push_back(node) ;
    for(ll i = 0 ; i < g1[node].size() ; i++)
    {
        if(!vis[g1[node][i]])
            dfs1(g1[node][i]) ;
    }
}
#define pi pair<pair<int , char>, pair<int , int> >
ll ncr(ll n , ll r)
{
    long long p = 1, k = 1;

    if (n - r < r)
        r = n - r;

    if (r != 0) {
        while (r) {
            p *= n;
            k *= r;

            long long m = __gcd(p, k);

            p /= m;
            k /= m;

            n--;
            r--;
        }

    }

    else
        p = 1;

    return p ;

}
int main()
{

   ll n ;
   cin>>n ;

   vector<ll> a(n + 1) ;

   for(ll i = 1 ; i <= n ; i++)
   {
       cin>>a[i] ;
       g[i].push_back(a[i]) ;
       g1[a[i]].push_back(i) ;
   }

   for(ll i = 1 ; i <= n ; i++)
   {
       if(!vis[i])
        dfs(i) ;
   }
   for(ll i = 0 ; i <= 200000 ; i++)
       vis[i] = false ;

   while(!s.empty())
   {
       ll x = s.top() ;
       s.pop() ;

       if(!vis[x])
       {
           dfs1(x) ;
           cycle.push_back(cc) ;
           cc.clear() ;
       }
   }
  ll ans  = 1 ;
   for(ll i = 0 ; i < cycle.size() ; i++)
   {
      if(cycle[i].size() == 1)
      {

          ans = ((ans*2)  +  mod )%mod;

      }
      else
      {
          ll sum = 0 ;
          for(ll j =  1 ; j < cycle[i].size() ; j++)
          {
              sum += ncr(cycle[i].size() , j ) ;
          }

          ans = ((ans*(sum)) + mod)%mod ;

      }

   }

   cout<<ans ;

}


#define ll long long int
#include<bits/stdc++.h>
using namespace std ;

#define mod 1000000007
vector<vector<ll> > g(200001) ;
vector<vector<ll> > g1(200001) ;
vector<bool> vis(200001 , false) ;
stack<ll> s ;
vector<vector<ll> > cycle ;
void dfs(ll node)
{
    vis[node] = true ;

    for(ll i = 0 ; i < g[node].size() ; i++)
    {
        if(!vis[g[node][i]])
            dfs(g[node][i]) ;
    }

    s.push(node) ;
}

ll cnt = 0 ;
void dfs1(ll node)
{
    vis[node] = true ;
    cnt++ ;
    for(ll i = 0 ; i < g1[node].size() ; i++)
    {
        if(!vis[g1[node][i]])
            dfs1(g1[node][i]) ;
    }
}
#define pi pair<pair<int , char>, pair<int , int> >
ll ncr(ll n, ll r, ll p)
{
    // Optimization for the cases when r is large
    if (r > n - r)

        r = n - r;

    // The array C is going to store last row of
    // pascal triangle at the end. And last entry
    // of last row is nCr
    ll C[r + 1];
    memset(C, 0, sizeof(C));

    C[0] = 1; // Top row of Pascal Triangle

    // One by constructs remaining rows of Pascal
    // Triangle from top to bottom
    for (ll i = 1; i <= n; i++) {

        // Fill entries of current row using previous
        // row values
        for (ll j = min(i, r); j > 0; j--)

            // nCj = (n-1)Cj + (n-1)C(j-1);
            C[j] = (C[j] + C[j - 1]) % p;
    }
    return C[r];
}
int main()
{

   ll n ;
   cin>>n ;

   vector<ll> a(n + 1) ;

   for(ll i = 1 ; i <= n ; i++)
   {
       cin>>a[i] ;
       g[i].push_back(a[i]) ;
       g1[a[i]].push_back(i) ;
   }

   for(ll i = 1 ; i <= n ; i++)
   {
       if(!vis[i])
        dfs(i) ;
   }
   for(ll i = 0 ; i <= 200000 ; i++)
       vis[i] = false ;

   ll ans  =1 ;
   while(!s.empty())
   {
       ll x = s.top() ;
       s.pop() ;

       if(!vis[x])
       {
           dfs1(x) ;
           if(cnt == 1)
           {
               ans = (ans*2)%mod ;
           }
           else
           {
               ll sum = 0 ;
               for(ll i = 1 ; i < cnt ; i++)
                sum += ncr(cnt , i , mod) ;

               ans = (ans*sum)%mod ;
           }
           cnt = 0 ;
       }
   }

   cout<<ans ;

}


#define ll long long int
#include<bits/stdc++.h>
using namespace std ;

#define mod 1000000007
vector<vector<ll> > g(200001) ;
vector<vector<ll> > g1(200001) ;
vector<bool> vis(200001 , false) ;
stack<ll> s ;
vector<vector<ll> > cycle ;
void dfs(ll node)
{
    vis[node] = true ;

    for(ll i = 0 ; i < g[node].size() ; i++)
    {
        if(!vis[g[node][i]])
            dfs(g[node][i]) ;
    }

    s.push(node) ;
}

ll cnt = 0 ;
void dfs1(ll node)
{
    vis[node] = true ;
    cnt++ ;
    for(ll i = 0 ; i < g1[node].size() ; i++)
    {
        if(!vis[g1[node][i]])
            dfs1(g1[node][i]) ;
    }
}
#define pi pair<pair<int , char>, pair<int , int> >
ll ncr(ll n, ll r)
{

    // p holds the value of n*(n-1)*(n-2)...,
    // k holds the value of r*(r-1)...
    ll p = 1, k = 1;

    // C(n, r) == C(n, n-r),
    // choosing the smaller value
    if (n - r < r)
        r = n - r;

    if (r != 0) {
        while (r) {

            p = p%mod ;
            k = k%mod ;
            p = (p*n)%mod;
            k = (k*r)%mod;

            // gcd of p, k
            ll m = __gcd(p, k)%mod;

            // dividing by gcd, to simplify product
            // division by their gcd saves from the overflow
            p /= m;
            k /= m;

            n--;
            r--;
        }

        // k should be simplified to 1
        // as C(n, r) is a natural number
        // (denominator should be 1 ) .
    }

    else
        p = 1;

    // if our approach is correct p = ans and k =1
  return p ;
}
ll binpow(ll x,ll y){
    x = x%mod;
    ll res = 1;
    while(y){
        if(y&1) res = (res*x)%mod;
        x = (x*x)%mod;
        y /= 2;
    }
    return res;
}
int main()
{

   ll n ;
   cin>>n ;

   vector<ll> a(n + 1) ;

   for(ll i = 1 ; i <= n ; i++)
   {
       cin>>a[i] ;
       g[i].push_back(a[i]) ;
       g1[a[i]].push_back(i) ;
   }

   for(ll i = 1 ; i <= n ; i++)
   {
       if(!vis[i])
        dfs(i) ;
   }
   for(ll i = 0 ; i <= 200000 ; i++)
       vis[i] = false ;

   ll ans  =1 ;
   while(!s.empty())
   {
       ll x = s.top() ;
       s.pop() ;

       if(!vis[x])
       {
           dfs1(x) ;
           if(cnt == 1)
           {
               ans = (ans*2)%mod ;
           }
           else
           {
               ll sum = 0 ;
               for(ll i = 1 ; i < cnt ; i++)
                sum += ncr(cnt , i ) ;

               ans = (ans*(sum + mod))%mod ;
           }
           cnt = 0 ;
       }
   }

   cout<<ans ;

}


#define ll long long int
#include<bits/stdc++.h>
using namespace std ;

#define mod 1000000007
vector<vector<ll> > g(200001) ;
vector<vector<ll> > g1(200001) ;
vector<bool> vis(200001 , false) ;
stack<ll> s ;
vector<vector<ll> > cycle ;
void dfs(ll node)
{
    vis[node] = true ;

    for(ll i = 0 ; i < g[node].size() ; i++)
    {
        if(!vis[g[node][i]])
            dfs(g[node][i]) ;
    }

    s.push(node) ;
}

vector<ll> cc ;
void dfs1(ll node)
{
    vis[node] = true ;
    cc.push_back(node) ;
    for(ll i = 0 ; i < g1[node].size() ; i++)
    {
        if(!vis[g1[node][i]])
            dfs1(g1[node][i]) ;
    }
}
#define pi pair<pair<int , char>, pair<int , int> >
ll ncr(ll n , ll r)
{
    long long p = 1, k = 1;

    if (n - r < r)
        r = n - r;

    if (r != 0) {
        while (r) {
            p *= n;
            k *= r;

            long long m = __gcd(p, k);

            p /= m;
            k /= m;

            n--;
            r--;
        }

    }

    else
        p = 1;

    return p ;

}
int main()
{

   ll n ;
   cin>>n ;

   vector<ll> a(n + 1) ;

   for(ll i = 1 ; i <= n ; i++)
   {
       cin>>a[i] ;
       g[i].push_back(a[i]) ;
       g1[a[i]].push_back(i) ;
   }

   for(ll i = 1 ; i <= n ; i++)
   {
       if(!vis[i])
        dfs(i) ;
   }
   for(ll i = 0 ; i <= 200000 ; i++)
       vis[i] = false ;

   while(!s.empty())
   {
       ll x = s.top() ;
       s.pop() ;

       if(!vis[x])
       {
           dfs1(x) ;
           cycle.push_back(cc) ;
           cc.clear() ;
       }
   }
  ll ans  = 1 ;
   for(ll i = 0 ; i < cycle.size() ; i++)
   {
      if(cycle[i].size() == 1)
      {

          ans = ((ans*2))%mod;

      }
      else
      {
          ll sum = 0 ;
          for(ll j =  1 ; j < cycle[i].size() ; j++)
          {
              sum += ncr(cycle[i].size() , j ) ;
          }

          sum = sum%mod ;
          ans = (ans*(sum))%mod ;

      }

      ans = ans%mod ;

   }

   cout<<ans ;

}


#define ll long long int
#include<bits/stdc++.h>
using namespace std ;

#define mod 1000000007
vector<vector<ll> > g(200001) ;
vector<vector<ll> > g1(200001) ;
vector<bool> vis(200001 , false) ;
stack<ll> s ;
vector<vector<ll> > cycle ;
void dfs(ll node)
{
    vis[node] = true ;

    for(ll i = 0 ; i < g[node].size() ; i++)
    {
        if(!vis[g[node][i]])
            dfs(g[node][i]) ;
    }

    s.push(node) ;
}

vector<ll> cc ;
void dfs1(ll node)
{
    vis[node] = true ;
    cc.push_back(node) ;
    for(ll i = 0 ; i < g1[node].size() ; i++)
    {
        if(!vis[g1[node][i]])
            dfs1(g1[node][i]) ;
    }
}
#define pi pair<pair<int , char>, pair<int , int> >
ll ncr(ll n , ll r)
{
    long long p = 1, k = 1;

    if (n - r < r)
        r = n - r;

    if (r != 0) {
        while (r) {
            p *= n;
            k *= r;

            long long m = __gcd(p, k);

            p /= m;
            k /= m;

            n--;
            r--;
        }

    }

    else
        p = 1;

    return p ;

}
int main()
{

   ll n ;
   cin>>n ;

   vector<ll> a(n + 1) ;

   for(ll i = 1 ; i <= n ; i++)
   {
       cin>>a[i] ;
       g[i].push_back(a[i]) ;
       g1[a[i]].push_back(i) ;
   }

   for(ll i = 1 ; i <= n ; i++)
   {
       if(!vis[i])
        dfs(i) ;
   }
   for(ll i = 0 ; i <= 200000 ; i++)
       vis[i] = false ;

   while(!s.empty())
   {
       ll x = s.top() ;
       s.pop() ;

       if(!vis[x])
       {
           dfs1(x) ;
           cycle.push_back(cc) ;
           cc.clear() ;
       }
   }
  ll ans  = 1 ;
   for(ll i = 0 ; i < cycle.size() ; i++)
   {
      if(cycle[i].size() == 1)
      {

          ans = ((ans*2))%mod;

      }
      else
      {
          ll sum = 0 ;
          for(ll j =  1 ; j < cycle[i].size() ; j++)
          {
              sum += ncr(cycle[i].size() , j )%mod ;
          }

          ans = (ans*(sum))%mod ;

      }

      ans = ans%mod ;

   }

   cout<<ans ;

}


#define ll long long int
#include<bits/stdc++.h>
using namespace std ;

#define mod 1000000007
vector<vector<ll> > g(200001) ;
vector<vector<ll> > g1(200001) ;
vector<bool> vis(200001 , false) ;
stack<ll> s ;
vector<vector<ll> > cycle ;
void dfs(ll node)
{
    vis[node] = true ;

    for(ll i = 0 ; i < g[node].size() ; i++)
    {
        if(!vis[g[node][i]])
            dfs(g[node][i]) ;
    }

    s.push(node) ;
}

vector<ll> cc ;
void dfs1(ll node)
{
    vis[node] = true ;
    cc.push_back(node) ;
    for(ll i = 0 ; i < g1[node].size() ; i++)
    {
        if(!vis[g1[node][i]])
            dfs1(g1[node][i]) ;
    }
}
#define pi pair<pair<int , char>, pair<int , int> >
ll ncr(ll n , ll r)
{
    long long p = 1, k = 1;

    if (n - r < r)
        r = n - r;

    if (r != 0) {
        while (r) {
            p *= n;
            p = p%mod ;
            k *= r;
            k = k%mod ;

            long long m = __gcd(p, k);

            p /= m;
            k /= m;

            n--;
            r--;
        }

    }

    else
        p = 1;

    return p ;

}
int main()
{

   ll n ;
   cin>>n ;

   vector<ll> a(n + 1) ;

   for(ll i = 1 ; i <= n ; i++)
   {
       cin>>a[i] ;
       g[i].push_back(a[i]) ;
       g1[a[i]].push_back(i) ;
   }

   for(ll i = 1 ; i <= n ; i++)
   {
       if(!vis[i])
        dfs(i) ;
   }
   for(ll i = 0 ; i <= 200000 ; i++)
       vis[i] = false ;

   while(!s.empty())
   {
       ll x = s.top() ;
       s.pop() ;

       if(!vis[x])
       {
           dfs1(x) ;
           cycle.push_back(cc) ;
           cc.clear() ;
       }
   }
  ll ans  = 1 ;
   for(ll i = 0 ; i < cycle.size() ; i++)
   {
      if(cycle[i].size() == 1)
      {

          ans = ((ans*2))%mod;

      }
      else
      {
          ll sum = 0 ;
          for(ll j =  1 ; j < cycle[i].size() ; j++)
          {
              sum += ncr(cycle[i].size() , j ) ;
          }

          ans = (ans*(sum))%mod ;

      }

      ans = ans%mod ;

   }

   cout<<ans ;

}


#define ll long long int
#include<bits/stdc++.h>
using namespace std ;

#define mod 1000000007
vector<vector<ll> > g(200001) ;
vector<vector<ll> > g1(200001) ;
vector<bool> vis(200001 , false) ;
stack<ll> s ;
vector<vector<ll> > cycle ;
void dfs(ll node)
{
    vis[node] = true ;

    for(ll i = 0 ; i < g[node].size() ; i++)
    {
        if(!vis[g[node][i]])
            dfs(g[node][i]) ;
    }

    s.push(node) ;
}

ll cnt = 0 ;
void dfs1(ll node)
{
    vis[node] = true ;
    cnt++ ;
    for(ll i = 0 ; i < g1[node].size() ; i++)
    {
        if(!vis[g1[node][i]])
            dfs1(g1[node][i]) ;
    }
}
#define pi pair<pair<int , char>, pair<int , int> >
ll ncr(ll n, ll r)
{

    // p holds the value of n*(n-1)*(n-2)...,
    // k holds the value of r*(r-1)...
    ll p = 1, k = 1;

    // C(n, r) == C(n, n-r),
    // choosing the smaller value
    if (n - r < r)
        r = n - r;

    if (r != 0) {
        while (r) {

            p = p%mod ;
            k = k%mod ;
            p = (p*n)%mod;
            k = (k*r)%mod;

            // gcd of p, k
            ll m = __gcd(p, k);

            // dividing by gcd, to simplify product
            // division by their gcd saves from the overflow
            p /= m;
            k /= m;

            n--;
            r--;
        }

        // k should be simplified to 1
        // as C(n, r) is a natural number
        // (denominator should be 1 ) .
    }

    else
        p = 1;

    // if our approach is correct p = ans and k =1
  return p ;
}
ll binpow(ll x,ll y){
    x = x%mod;
    ll res = 1;
    while(y){
        if(y&1) res = (res*x)%mod;
        x = (x*x)%mod;
        y /= 2;
    }
    return res;
}
int main()
{

   ll n ;
   cin>>n ;

   vector<ll> a(n + 1) ;

   for(ll i = 1 ; i <= n ; i++)
   {
       cin>>a[i] ;
       g[i].push_back(a[i]) ;
       g1[a[i]].push_back(i) ;
   }

   for(ll i = 1 ; i <= n ; i++)
   {
       if(!vis[i])
        dfs(i) ;
   }
   for(ll i = 0 ; i <= 200000 ; i++)
       vis[i] = false ;

   ll ans  =1 ;
   while(!s.empty())
   {
       ll x = s.top() ;
       s.pop() ;

       if(!vis[x])
       {
           dfs1(x) ;
           if(cnt == 1)
           {
               ans = (ans*2)%mod ;
           }
           else
           {
               ll sum = 0 ;
               for(ll i = 1 ; i < cnt ; i++)
                sum += ncr(cnt , i ) ;

               ans = (ans*(sum + mod))%mod ;
           }
           cnt = 0 ;
       }
   }

   cout<<ans ;

}


#define ll long long int
#include<bits/stdc++.h>
using namespace std ;

#define mod 1000000007
vector<vector<ll> > g(200001) ;
vector<vector<ll> > g1(200001) ;
vector<bool> vis(200001 , false) ;
stack<ll> s ;
vector<vector<ll> > cycle ;
void dfs(ll node)
{
    vis[node] = true ;

    for(ll i = 0 ; i < g[node].size() ; i++)
    {
        if(!vis[g[node][i]])
            dfs(g[node][i]) ;
    }

    s.push(node) ;
}

ll cnt = 0 ;
void dfs1(ll node)
{
    vis[node] = true ;
    cnt++ ;
    for(ll i = 0 ; i < g1[node].size() ; i++)
    {
        if(!vis[g1[node][i]])
            dfs1(g1[node][i]) ;
    }
}
#define pi pair<pair<int , char>, pair<int , int> >
ll ncr(ll n, ll r, ll p)
{
    // Optimization for the cases when r is large
    if (r > n - r)

        r = n - r;

    // The array C is going to store last row of
    // pascal triangle at the end. And last entry
    // of last row is nCr
    ll C[r + 1];
    memset(C, 0, sizeof(C));

    C[0] = 1; // Top row of Pascal Triangle

    // One by constructs remaining rows of Pascal
    // Triangle from top to bottom
    for (ll i = 1; i <= n; i++) {

        // Fill entries of current row using previous
        // row values
        for (ll j = min(i, r); j > 0; j--)

            // nCj = (n-1)Cj + (n-1)C(j-1);
            C[j] = (C[j] + C[j - 1]) % p;
    }
    return C[r];
}
int main()
{

   ll n ;
   cin>>n ;

   vector<ll> a(n + 1) ;

   for(ll i = 1 ; i <= n ; i++)
   {
       cin>>a[i] ;
       g[i].push_back(a[i]) ;
       g1[a[i]].push_back(i) ;
   }

   for(ll i = 1 ; i <= n ; i++)
   {
       if(!vis[i])
        dfs(i) ;
   }
   for(ll i = 0 ; i <= 200000 ; i++)
       vis[i] = false ;

   ll ans  =1 ;
   while(!s.empty())
   {
       ll x = s.top() ;
       s.pop() ;

       if(!vis[x])
       {
           dfs1(x) ;
           if(cnt == 1)
           {
               ans = (ans*2)%mod ;
           }
           else
           {
               ll sum = 0 ;
               for(ll i = 1 ; i < cnt ; i++)
                sum += ncr(cnt , i , mod) ;

               ans = (ans*(sum + mod))%mod ;
           }
           cnt = 0 ;
       }
   }

   cout<<ans ;

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int n,s,k;
int a[200010],d[200010],cnt=0,dem=0;
int c[200010],chk[200010];
vector<int> b[200010];
ll p = 1e9+7,ans;
ll lv(ll x, ll y){
	ll r = 1;
	while(y){
		if(y&1) r = (r*x)%p;
		x = (x*x)%p;
		y >>= 1;
	}
	return r;
}
void dfs(int u){
	c[u] = dem;
	cnt++;
	int v = a[u];
	if(c[v]!=0&&c[v]!=dem) return;
	if(d[v]==0){
		d[v] = d[u] + 1;
		dfs(v);
	}else{
		k = d[u] - d[v] + 1;
		return;
	}
}
int main(){
	//freopen("in.in","r",stdin);
	cin >> n;
	for(int i = 1; i <= n; i++){
		cin >> a[i];
		chk[a[i]] = 1;
	}
	ans = 1;
	for(int i = 1; i <= n; i++){
		if(chk[i]==0){
			++dem;
			cnt = 0;
			k = 0;
			d[i] = 1;
			dfs(i);
			if(k>=2) ans = (ans*(lv(2,k)-2))%p;
			ans = (ans * (lv(2,cnt-k))%p)%p;
		}
	}
	for(int i = 1; i <= n; i++){
		if(c[i]==0){
			++dem;
			cnt = 0;
			k = 0;
			d[i] = 1;
			dfs(i);
			ans = (ans * ((lv(2,k)-2)*(lv(2,cnt-k)))%p)%p;
		}
	}
	cout << ans;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int n,s,k;
int a[200010],d[200010];
bool c[200010];
ll p = 1e9+7;
ll lv(ll x, ll y){
	ll r = 1;
	while(y){
		if(y&1) r = (r*x)%p;
		x = (x*x)%p;
		y >>= 1;
	}
	return r;
}
void dfs(int u){
	int v = a[u];
	if(d[v]==0){
		d[v] = d[u] + 1;
		dfs(v);
	}else{
		k = d[u] - d[v] + 1;
		return;
	}
}
int main(){
	//freopen("in.in","r",stdin);
	cin >> n;
	for(int i = 1; i <= n; i++){
		cin >> a[i];
		if(c[a[i]]==0){
			c[a[i]] = 1;
		}
	}
	for(int i = 1; i <= n; i++){
		if(c[i]==0){
			s = i;
			break;
		}
	}
	if(s==0){
		cout << (lv(2,n)-2+p)%p;
	}else{
		d[s] = 1;
		dfs(s);
		//cout << k << "\n";
		ll ans = (lv(2,k)-2+p)%p;
		cout << ans*lv(2,n-k)%p;
	}
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int n,s,k;
int a[200010],d[200010];
bool c[200010];
ll p = 1e9+7;
ll lv(ll x, ll y){
	ll r = 1;
	while(y){
		if(y&1) r = (r*x)%p;
		x = (x*x)%p;
		y >>= 1;
	}
	return r;
}
void dfs(int u){
	int v = a[u];
	if(d[v]==0){
		d[v] = d[u] + 1;
		dfs(v);
	}else{
		k = d[u] - d[v] + 1;
		return;
	}
}
int main(){
	//freopen("in.in","r",stdin);
	cin >> n;
	for(int i = 1; i <= n; i++){
		cin >> a[i];
		if(c[a[i]]==0){
			c[a[i]] = 1;
		}
	}
	for(int i = 1; i <= n; i++){
		if(c[i]==0){
			s = i;
			break;
		}
	}
	if(s==0){
		cout << (lv(2,n)-2+p)%p;
	}else{
		d[s] = 1;
		dfs(s);
		//cout << k << "\n";
		ll ans = (lv(2,k)-2+p)%p;
		cout << ans*(n-k+1)%p;
	}
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int n;
string s;
int memo[5100][5100];
int f[5100][5100];
int p = 1e9+7;
ll H[5100],M[5100];
int match(int x, int y){
	x++;
	y++;
	int l = -1, r = n-y+1, mid;
	while(r-l>1){
		int mid = (l+r)/2;
		ll h1 = H[x+mid-1]-H[x-1]*M[mid];
		ll h2 = H[y+mid-1]-H[y-1]*M[mid];
		if(h1==h2) l = mid;
		else r = mid;
	}
	return l;
}
int solve(int x, int l){
	if(s[x]=='0') return 0;
	if(x==n) return 1;
	if(x+l>n) return 0;
	if(memo[x][l]!=-1) return memo[x][l];
	int cur = solve(x,l+1);
	int d = match(x,x+l);
	if(d<l&&s[x+d]<s[x+l+d]) cur = (cur+solve(x+l,l))%p;
	else cur = (cur+solve(x+l,l+1))%p;
	return memo[x][l] = cur;
}
int main(){
	memset(memo,-1,sizeof(memo));
	memset(f,-1,sizeof(f));
	cin >> n;
	cin >> s;
	M[0] = 1;
	for(int i = 1; i <= n; i++) M[i] = M[i-1]*1999;
	for(int i = 1; i <= n; i++) H[i] = H[i-1]*1999+s[i-1];
	cout << solve(0,1);
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int n,s,k;
int a[200010],d[200010];
bool c[200010];
ll p = 1e9+7;
ll lv(ll x, ll y){
	ll r = 1;
	while(y){
		if(y&1) r = (r*x)%p;
		x = (x*x)%p;
		y >>= 1;
	}
	return r;
}
void dfs(int u){
	int v = a[u];
	if(d[v]==0){
		d[v] = d[u] + 1;
		dfs(v);
	}else{
		k = d[u] - d[v] + 1;
		return;
	}
}
int main(){
	freopen("in.in","r",stdin);
	cin >> n;
	for(int i = 1; i <= n; i++){
		cin >> a[i];
		if(c[a[i]]==0){
			c[a[i]] = 1;
		}
	}
	for(int i = 1; i <= n; i++){
		if(c[i]==0){
			s = i;
			break;
		}
	}
	if(s==0){
		cout << (lv(2,n)-2+p)%p;
	}else{
		d[s] = 1;
		dfs(s);
		//cout << k << "\n";
		ll ans = (lv(2,k)-2+p)%p;
		cout << ans*(n-k+1)%p;
	}
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int n,s,k;
int a[200010],d[200010],cnt=0,dem=0;
int c[200010],g[200010],chk[200010];
vector<int> b[200010];
ll p = 1e9+7,ans;
ll lv(ll x, ll y){
	ll r = 1;
	while(y){
		if(y&1) r = (r*x)%p;
		x = (x*x)%p;
		y >>= 1;
	}
	return r;
}
void dfs(int u){
	int v = a[u];
	if(d[v]==0){
		d[v] = d[u] + 1;
		dfs(v);
	}else{
		k = d[u] - d[v] + 1;
		return;
	}
}
void find(int u){
	++cnt;
	if(chk[u]==0) s = u;
	g[u] = dem;
	c[u] = 1;
	for(int i = 0; i < b[u].size(); i++){
		int v = b[u][i];
		if(c[v]==1) continue;
		//cout << v << "\n";
		find(v);
	}
}
int main(){
	//freopen("in.in","r",stdin);
	cin >> n;
	for(int i = 1; i <= n; i++){
		cin >> a[i];
		chk[a[i]] = 1;
		b[i].push_back(a[i]);
		b[a[i]].push_back(i);
	}
	for(int i = 1; i <= n; i++){
		++dem;
		if(c[i]==0){
			s = 0;
			cnt = 0;
			find(i);
			if(s==0){
				ans = (ans + lv(2,cnt)-2)%p;
			}else{
				k = 0;
				dfs(s);
				//cout << k << "\n";
				ans = (ans + (lv(2,k)-2)*(lv(2,cnt-k))%p)%p;
			}
		}
	}
	cout << ans;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<iostream>
using namespace std;
const int mod=1000000007;
typedef long long LL;
vector<int> g[200005];
int vis[200005],step[200005];
int k,num[200005],f[200005];
int find(int x){
    return (f[x]==x)?x:(f[x]=find(f[x]));
}
void merge(int x,int y){
    int fx=find(x);
    int fy=find(y);
    if(fx>fy) swap(fx,fy);
    f[fy]=fx;
}
LL pow_mod(int x,int n){
    LL res=1,t=x;
    while(n){
        if(n&1) res=(res*t)%mod;
        n/=2;
        t=(t*t)%mod;
    }
    return res;
}
void dfs(int x,int s){
    step[x]=s;
    vis[x]=1;
    for(int i=0;i<g[x].size();i++){
        int v=g[x][i];
        if(vis[v]){
            k+=step[x]-step[v]+1;
            return;
        }
        else{
            dfs(v,s+1);
        }
    }
}
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) f[i]=i;
    for(int i=1;i<=n;i++){
        int a; scanf("%d",&a);
        g[i].push_back(a);
        merge(i,a);
    }
    memset(vis,0,sizeof(vis));
    memset(num,0,sizeof(num));
    memset(step,0,sizeof(step));
    for(int i=1;i<=n;i++){
        num[find(i)]++;
    }
    LL ans=1;
    for(int i=1;i<=n;i++){
        if(find(i)==i){
            k=0;
            dfs(i,0);
            if(k!=0)
            ans=(mod+(ans*(pow_mod(2,num[i]-k)*(pow_mod(2,k)-2))%mod)%mod)%mod;
        }
    }
    cout<<ans<<endl;

    return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<iostream>
using namespace std;
typedef long long LL;
const LL mod=1000000007;
vector<LL> g[200005];
LL vis[200005],step[200005];
LL k,num[200005],f[200005];
LL find(LL x){
    return (f[x]==x)?x:(f[x]=find(f[x]));
}
void merge(LL x,LL y){
    LL fx=find(x);
    LL fy=find(y);
    if(fx>fy) swap(fx,fy);
    f[fy]=fx;
}
LL pow_mod(LL x,LL n){
    LL res=1,t=x;
    while(n){
        if(n&1) res=(res*t)%mod ;
        n/=2;
        t=(t*t)%mod;
    }
    return res;
}
void dfs(LL x,LL s){
    step[x]=s;
    vis[x]=1;
    for(LL i=0;i<g[x].size();i++){
        LL v=g[x][i];
        if(vis[v]){
            k=step[x]-step[v]+1;
            return;
        }
        else{
            dfs(v,s+1);
        }
    }
}

int main(){
    LL n;
    scanf("%d",&n);
    for(LL i=1;i<=n;i++) f[i]=i;
    for(LL i=1;i<=n;i++){
        LL a; scanf("%d",&a);
        g[i].push_back(a);
        merge(i,a);
    }
    memset(vis,0,sizeof(vis));
    memset(num,0,sizeof(num));
    memset(step,0,sizeof(step));
    for(
        LL i=1;i<=n;i++){
        num[find(i)]++;
    }
    LL ans=1LL;
    for(LL i=1;i<=n;i++){
        //printf("num[%d]=%d\n",i,num[i]);
        if(!vis[i]){
            k=0;
            dfs(i,0);
            if(k>1)
                ans=(ans*(pow_mod(2,num[find(i)]-k)*(pow_mod(2,k)-2))%mod)%mod;
        }
    }
    cout<<ans<<endl;

    return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<iostream>
using namespace std;
const int mod=1000000007;
typedef long long LL;
vector<int> g[200005];
int vis[200005],step[200005];
int k,num[200005],f[200005];
int find(int x){
    return (f[x]==x)?x:(f[x]=find(f[x]));
}
void merge(int x,int y){
    int fx=find(x);
    int fy=find(y);
    if(fx>fy) swap(fx,fy);
    f[fy]=fx;
}
LL pow_mod(int x,int n){
    LL res=1,t=x;
    while(n){
        if(n&1) res=(res*t)%mod;
        n/=2;
        t=(t*t)%mod;
    }
    return res;
}
void dfs(int x,int s){
    step[x]=s;
    vis[x]=1;
    for(int i=0;i<g[x].size();i++){
        int v=g[x][i];
        if(vis[v]){
            k=step[x]-step[v]+1;
            return;
        }
        else{
            dfs(v,s+1);
        }
    }
}
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) f[i]=i;
    for(int i=1;i<=n;i++){
        int a; scanf("%d",&a);
        g[i].push_back(a);
        merge(i,a);
    }
    memset(vis,0,sizeof(vis));
    memset(num,0,sizeof(num));
    memset(step,0,sizeof(step));
    for(int i=1;i<=n;i++){
        num[find(i)]++;
    }
    LL ans=1;
    for(int i=1;i<=n;i++){
        if(num[i]){
            k=0;
            dfs(i,0);
            ans=(mod+(ans*(pow_mod(2,num[i]-k)*(pow_mod(2,k)-2))%mod)%mod)%mod;
        }
    }
    cout<<ans<<endl;

    return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<iostream>
using namespace std;
const int mod=1000000007;
typedef long long LL;
vector<int> g[200005];
int vis[200005],step[200005];
int k,num[200005],f[200005];
int find(int x){
    return (f[x]==x)?x:(f[x]=find(f[x]));
}
void merge(int x,int y){
    int fx=find(x);
    int fy=find(y);
    if(fx>fy) swap(fx,fy);
    f[fy]=fx;
}
LL pow_mod(int x,int n){
    LL res=1,t=x;
    while(n){
        if(n&1) res=(res*t)%mod;
        n/=2;
        t=(t*t)%mod;
    }
    return res;
}
void dfs(int x,int s){
    step[x]=s;
    vis[x]=1;
    for(int i=0;i<g[x].size();i++){
        int v=g[x][i];
        if(vis[v]){
            k+=step[x]-step[v]+1;
            return;
        }
        else{
            dfs(v,s+1);
        }
    }
}
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) f[i]=i;
    for(int i=1;i<=n;i++){
        int a; scanf("%d",&a);
        g[i].push_back(a);
        merge(i,a);
    }
    memset(vis,0,sizeof(vis));
    memset(num,0,sizeof(num));
    memset(step,0,sizeof(step));
    for(int i=1;i<=n;i++){
        num[find(i)]++;
    }
    LL ans=1;
    for(int i=1;i<=n;i++){
        if(find(i)==i){
            k=0;
            dfs(i,0);
            if(k!=0)
            ans=(ans*(pow_mod(2,num[i]-k)*(pow_mod(2,k)-2))%mod)%mod;
        }
    }
    cout<<ans<<endl;

    return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<iostream>
using namespace std;
typedef long long LL;
const LL mod=1000000007;
vector<LL> g[200005];
LL vis[200005],step[200005];
LL k,num[200005],f[200005];
LL find(LL x){
    return (f[x]==x)?x:(f[x]=find(f[x]));
}
void merge(LL x,LL y){
    LL fx=find(x);
    LL fy=find(y);
    if(fx>fy) swap(fx,fy);
    f[fy]=fx;
}
LL pow_mod(LL x,LL n){
    LL res=1,t=x;
    while(n){
        if(n&1) res=(res*t)%mod;
        n/=2;
        t=(t*t)%mod;
    }
    return res;
}
void dfs(LL x,LL s){
    step[x]=s;
    vis[x]=1;
    for(LL i=0;i<g[x].size();i++){
        LL v=g[x][i];
        if(vis[v]){
            k=step[x]-step[v]+1;
            return;
        }
        else{
            dfs(v,s+1);
        }
    }
}

int main(){
    LL n;
    scanf("%lld",&n);
    for(LL i=1;i<=n;i++) f[i]=i;
    for(LL i=1;i<=n;i++){
        LL a; scanf("%lld",&a);
        g[i].push_back(a);
        merge(i,a);
    }
    memset(vis,0,sizeof(vis));
    memset(num,0,sizeof(num));
    memset(step,0,sizeof(step));
    for(LL i=1;i<=n;i++){
        num[find(i)]++;
    }
    LL ans=1LL;
    for(LL i=1;i<=n;i++){
        //printf("num[%d]=%d\n",i,num[i]);
        if(!vis[i]){
            k=0;
            dfs(i,0);
            if(k>1)
                ans=(ans*(pow_mod(2,num[find(i)]-k)*(pow_mod(2,k)-2))%mod)%mod;
        }
    }
    cout<<ans<<endl;

    return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<iostream>
using namespace std;
const int mod=1000000007;
typedef long long LL;
vector<int> g[200005];
int vis[200005],step[200005];
int k,num[200005],f[200005];
int find(int x){
    return (f[x]==x)?x:(f[x]=find(f[x]));
}
void merge(int x,int y){
    int fx=find(x);
    int fy=find(y);
    if(fx>fy) swap(fx,fy);
    f[fy]=fx;
}
LL pow_mod(int x,int n){
    LL res=1,t=x;
    while(n){
        if(n&1) res=(res*t)%mod;
        n/=2;
        t=(t*t)%mod;
    }
    return res;
}
void dfs(int x,int s){
    step[x]=s;
    vis[x]=1;
    for(int i=0;i<g[x].size();i++){
        int v=g[x][i];
        if(vis[v]){
            k+=step[x]-step[v]+1;
            return;
        }
        else{
            dfs(v,s+1);
        }
    }
}
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) f[i]=i;
    for(int i=1;i<=n;i++){
        int a; scanf("%d",&a);
        g[i].push_back(a);
        merge(i,a);
    }
    memset(vis,0,sizeof(vis));
    memset(num,0,sizeof(num));
    memset(step,0,sizeof(step));
    for(int i=1;i<=n;i++){
        num[find(i)]++;
    }
    LL ans=1;
    for(int i=1;i<=n;i++){
        if(find(i)==i){
            k=0;
            dfs(i,0);
            ans=(ans*(pow_mod(2,num[i]-k)*(pow_mod(2,k)-2))%mod)%mod;
        }
    }
    cout<<ans<<endl;

    return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<iostream>
using namespace std;
const int mod=1000000007;
typedef long long LL;
vector<int> g[200005];
int vis[200005],step[200005];
int k,num[200005],f[200005];
int find(int x){
    return (f[x]==x)?x:(f[x]=find(f[x]));
}
void merge(int x,int y){
    int fx=find(x);
    int fy=find(y);
    if(fx>fy) swap(fx,fy);
    f[fy]=fx;
}
LL pow_mod(int x,int n){
    LL res=1,t=x;
    while(n){
        if(n&1) res=(res*t)%mod;
        n/=2;
        t=(t*t)%mod;
    }
    return res;
}
void dfs(int x,int s){
    step[x]=s;
    vis[x]=1;
    for(int i=0;i<g[x].size();i++){
        int v=g[x][i];
        if(vis[v]){
            k=step[x]-step[v]+1;
            return;
        }
        else{
            dfs(v,s+1);
        }
    }
}
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) f[i]=i;
    for(int i=1;i<=n;i++){
        int a; scanf("%d",&a);
        g[i].push_back(a);
        merge(i,a);
    }
    memset(vis,0,sizeof(vis));
    memset(num,0,sizeof(num));
    memset(step,0,sizeof(step));
    for(int i=1;i<=n;i++){
        num[find(i)]++;
    }
    LL ans=1LL;
    for(int i=1;i<=n;i++){
        if(!vis[i]){
            k=0;
            dfs(i,0);
            if(k>1)
                ans=(ans*(pow_mod(2,num[find(i)]-k)*(pow_mod(2,k)-2))%mod)%mod;
        }
    }
    cout<<ans<<endl;

    return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<iostream>
using namespace std;
typedef long long LL;
const LL mod=1000000007;
vector<LL> g[200005];
LL vis[200005],step[200005];
LL k,num[200005],f[200005];
LL find(LL x){
    return (f[x]==x)?x:(f[x]=find(f[x]));
}
void merge(LL x,LL y){
    LL fx=find(x);
    LL fy=find(y);
    if(fx>fy) swap(fx,fy);
    f[fy]=fx;
}
LL pow_mod(LL x,LL n){
    LL res=1,t=x;
    while(n){
        if(n&1) res=(res*t)%mod;
        n/=2;
        t=(t*t)%mod;
    }
    return res;
}
void dfs(LL x,LL s){
    step[x]=s;
    vis[x]=1;
    for(LL i=0;i<g[x].size();i++){
        LL v=g[x][i];
        if(vis[v]){
            k=step[x]-step[v]+1;
            return;
        }
        else{
            dfs(v,s+1);
        }
    }
}

int main(){
    LL n;
    scanf("%d",&n);
    for(LL i=1;i<=n;i++) f[i]=i;
    for(LL i=1;i<=n;i++){
        LL a; scanf("%d",&a);
        g[i].push_back(a);
        merge(i,a);
    }
    memset(vis,0,sizeof(vis));
    memset(num,0,sizeof(num));
    memset(step,0,sizeof(step));
    for(
        LL i=1;i<=n;i++){
        num[find(i)]++;
    }
    LL ans=1LL;
    for(LL i=1;i<=n;i++){
        //printf("num[%d]=%d\n",i,num[i]);
        if(!vis[i]){
            k=0;
            dfs(i,0);
            if(k>1)
                ans=(ans*(pow_mod(2,num[find(i)]-k)*(pow_mod(2,k)-2))%mod)%mod;
        }
    }
    cout<<ans<<endl;

    return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<iostream>
using namespace std;
const int mod=1000000007;
typedef long long LL;
vector<int> g[200005];
int vis[200005],step[200005];
int k,num[200005],f[200005];
int find(int x){
    return (f[x]==x)?x:(f[x]=find(f[x]));
}
void merge(int x,int y){
    int fx=find(x);
    int fy=find(y);
    if(fx>fy) swap(fx,fy);
    f[fy]=fx;
}
LL pow_mod(int x,int n){
    LL res=1,t=x;
    while(n){
        if(n&1) res=(res*t)%mod;
        n/=2;
        t=(t*t)%mod;
    }
    return res;
}
void dfs(int x,int s){
    step[x]=s;
    vis[x]=1;
    for(int i=0;i<g[x].size();i++){
        int v=g[x][i];
        if(vis[v]){
            k=step[x]-step[v]+1;
            return;
        }
        else{
            dfs(v,s+1);
        }
    }
}
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) f[i]=i;
    for(int i=1;i<=n;i++){
        int a; scanf("%d",&a);
        g[i].push_back(a);
        merge(i,a);
    }
    memset(vis,0,sizeof(vis));
    memset(num,0,sizeof(num));
    memset(step,0,sizeof(step));
    for(int i=1;i<=n;i++){
        num[find(i)]++;
    }
    LL ans=1;
    for(int i=1;i<=n;i++){
        if(!vis[i]){
            k=0;
            dfs(i,0);
            if(k>1)
                ans=(ans*(pow_mod(2,num[find(i)]-k)*(pow_mod(2,k)-2))%mod)%mod;
        }
    }
    cout<<ans<<endl;

    return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<iostream>
using namespace std;
typedef long long LL;
const LL mod=1000000007;
vector<LL> g[200005];
LL vis[200005],step[200005];
LL k,num[200005],f[200005];
LL find(LL x){
    return (f[x]==x)?x:(f[x]=find(f[x]));
}
void merge(LL x,LL y){
    LL fx=find(x);
    LL fy=find(y);
    if(fx>fy) swap(fx,fy);
    f[fy]=fx;
}
LL pow_mod(int x,int n){
    LL res=1,t=x;
    while(n){
        if(n&1) res=(res*t)%mod;
        n/=2;
        t=(t*t)%mod;
    }
    return res;
}
void dfs(int x,int s,int fa){
    step[x]=s;
    vis[x]=1;
    for(int i=0;i<g[x].size();i++){
        int v=g[x][i];
        if(v==fa) continue;
        if(!vis[v]) dfs(v,s+1,x);
        else{
            //cout<<x<<" "<<v<<endl;
            if(k==0)
                k=step[x]-step[v]+1;
        }
    }
}

int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) f[i]=i;
    for(int i=1;i<=n;i++){
        int a; scanf("%d",&a);
        g[i].push_back(a);
        g[a].push_back(i);
        merge(i,a);
    }
    memset(vis,0,sizeof(vis));
    memset(num,0,sizeof(num));
    memset(step,0,sizeof(step));
    for(int i=1;i<=n;i++){
        num[find(i)]++;
    }
    for(int i=1;i<=n;i++){
        num[i]=num[find(i)];
    }
    LL ans=1LL;
    for(int i=1;i<=n;i++){
        if(!vis[i]){
            k=0;
            dfs(i,0,0);
            if(k==0) k=2;
            //cout<<"i="<<i<<endl;
            //cout<<"k="<<k<<endl;
            ans=(ans*(pow_mod(2,k)-2))%mod;
            ans=(ans*pow_mod(2,num[i]-k))%mod;
        }
    }
    cout<<ans<<endl;

    return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<iostream>
using namespace std;
const int mod=1000000007;
typedef long long LL;
vector<int> g[200005];
int vis[200005],step[200005];
int k,num[200005],f[200005];
int find(int x){
    return (f[x]==x)?x:(f[x]=find(f[x]));
}
void merge(int x,int y){
    int fx=find(x);
    int fy=find(y);
    if(fx>fy) swap(fx,fy);
    f[fy]=fx;
}
LL pow_mod(int x,int n){
    LL res=1,t=x;
    while(n){
        if(n&1) res=(res*t)%mod;
        n/=2;
        t=(t*t)%mod;
    }
    return res;
}
void dfs(int x,int s){
    step[x]=s;
    vis[x]=1;
    for(int i=0;i<g[x].size();i++){
        int v=g[x][i];
        if(vis[v]){
            k=step[x]-step[v]+1;
            return;
        }
        else{
            dfs(v,s+1);
        }
    }
}
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) f[i]=i;
    for(int i=1;i<=n;i++){
        int a; scanf("%d",&a);
        g[i].push_back(a);
        merge(i,a);
    }
    memset(vis,0,sizeof(vis));
    memset(num,0,sizeof(num));
    memset(step,0,sizeof(step));
    for(int i=1;i<=n;i++){
        num[find(i)]++;
    }
    LL ans=1;
    for(int i=1;i<=n;i++){
        if(!vis[i]){
            k=0;
            dfs(i,0);
            if(k>0)
                ans=(ans*(pow_mod(2,num[find(i)]-k)*(pow_mod(2,k)-2))%mod)%mod;
        }
    }
    cout<<ans<<endl;

    return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<iostream>
using namespace std;
const int mod=1000000007;
typedef long long LL;
vector<int> g[200005];
int vis[200005],step[200005];
int k;
LL pow_mod(int x,int n){
    LL res=1,t=x;
    while(n){
        if(n&1) res=(res*t)%mod;
        n/=2;
        t=(t*t)%mod;
    }
    return res;
}
void dfs(int x,int s){
    step[x]=s;
    vis[x]=1;
    for(int i=0;i<g[x].size();i++){
        int v=g[x][i];
        if(vis[v]){
            k=step[x]-step[v];
            return;
        }
        else{
            dfs(v,x+1);
        }
    }
}
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        int a; scanf("%d",&a);
        g[i].push_back(a);
    }
    dfs(1,0);
    LL ans=(pow_mod(2,n-k)*(pow_mod(2,k)-2))%mod;
    cout<<ans<<endl;

    return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<iostream>
using namespace std;
typedef long long LL;
const LL mod=1000000007;
vector<LL> g[200005];
LL vis[200005],step[200005];
LL k,num[200005],f[200005];
LL find(LL x){
    return (f[x]==x)?x:(f[x]=find(f[x]));
}
void merge(LL x,LL y){
    LL fx=find(x);
    LL fy=find(y);
    f[fy]=fx;
}
LL pow_mod(LL x,LL n){
    LL res=1,t=x;
    while(n){
        if(n&1) res=(res*t)%mod;
        n/=2;
        t=(t*t)%mod;
    }
    return res;
}
void dfs(LL x,LL s){
    step[x]=s;
    vis[x]=1;
    for(LL i=0;i<g[x].size();i++){
        LL v=g[x][i];
        if(vis[v]){
            k=step[x]-step[v]+1;
        }
        else{
            dfs(v,s+1);
        }
    }
}

int main(){
    LL n;
    scanf("%lld",&n);
    for(LL i=1;i<=n;i++) f[i]=i;
    for(LL i=1;i<=n;i++){
        LL a; scanf("%lld",&a);
        g[i].push_back(a);
        merge(i,a);
    }
    memset(vis,0,sizeof(vis));
    memset(num,0,sizeof(num));
    memset(step,0,sizeof(step));
    for(LL i=1;i<=n;i++){
        num[find(i)]++;
    }
    LL ans=1LL;
    for(LL i=1;i<=n;i++){
        if(!vis[i]){
            k=0;
            dfs(i,0);
            if(k>1)
                ans=(ans*(pow_mod(2,num[find(i)]-k)*(pow_mod(2,k)-2))%mod)%mod;
        }
    }
    cout<<ans<<endl;

    return 0;
}


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<iostream>
using namespace std;
const int mod=1000000007;
typedef long long LL;
vector<int> g[200005];
int vis[200005],step[200005];
int k,num[200005],f[200005];
int find(int x){
    return (f[x]==x)?x:(f[x]=find(f[x]));
}
void merge(int x,int y){
    int fx=find(x);
    int fy=find(y);
    if(fx>fy) swap(fx,fy);
    f[fy]=fx;
}
LL pow_mod(int x,int n){
    LL res=1,t=x;
    while(n){
        if(n&1) res=(res*t)%mod;
        n/=2;
        t=(t*t)%mod;
    }
    return res;
}
void dfs(int x,int s){
    step[x]=s;
    vis[x]=1;
    for(int i=0;i<g[x].size();i++){
        int v=g[x][i];
        if(vis[v]){
            k+=step[x]-step[v]+1;
            return;
        }
        else{
            dfs(v,s+1);
        }
    }
}
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++) f[i]=i;
    for(int i=1;i<=n;i++){
        int a; scanf("%d",&a);
        g[i].push_back(a);
        merge(i,a);
    }
    memset(vis,0,sizeof(vis));
    memset(num,0,sizeof(num));
    memset(step,0,sizeof(step));
    for(int i=1;i<=n;i++){
        num[find(i)]++;
    }
    LL ans=0;
    for(int i=1;i<=n;i++){
        if(!vis[i]){
            k=0;
            dfs(i,0);
            ans=(ans+pow_mod(2,num[i]-k)*(pow_mod(2,k)-2))%mod;
        }
    }
    cout<<ans<<endl;

    return 0;
}


#include<cstdio>
#include<cstring>
#include<vector>
#include<iostream>
#define N 200005
#define M (int)(1e9+7)
#define special 9
using namespace std;
typedef long long LL;
struct nod{
    LL edge;
    LL to;
    nod(LL a,LL b){
        edge=a;
        to=b;
    }
};
vector<nod>node[N];
LL n;
LL vis[N];
LL dfs(LL index,LL num){
    for(LL i=0;i<node[index].size();++i){
        LL e=node[index][i].edge,to=node[index][i].to;
        if(vis[e]==-1){
            vis[index]=to;
            LL temp=dfs(e,num+1);
            if(temp)return temp;
            vis[index]=-1;
        }else if(vis[e]==to){
            vis[index]=to;
            vis[e]=special;
            return num;
        }
    }
    return 0;
}
LL cls(LL index,LL num){
    for(LL i=0;i<node[index].size();++i){
        vis[index]=-1;
        LL e=node[index][i].edge;
        if(vis[e]==special)return num;
        if(vis[e]!=-1)
            return cls(e,num+1);
    }
    return 0;
}
LL pow(LL a,LL b){
    LL base=a,temp=1;
    while(b){
        if(b&1)temp=(temp*base)%M;
        base=(base*base)%M;
        b>>=1;
    }
    return temp;
}
LL mod(LL a,LL b){
    LL base=a,temp=0;
    while(b){
        if(b&1)temp=(temp+base)%M;
        base=(base+base)%M;
        b>>=1;
    }
    return temp;
}
int main(void){
    memset(vis,-1,sizeof(vis));
    LL res=0;
    scanf("%I64d",&n);
    for(LL i=1;i<=n;++i){
        LL vertice;
        cin>>vertice;
        scanf("%I64d",&vertice);
        node[i].push_back(nod(vertice,1));
        node[vertice].push_back(nod(i,0));
    }
    for(LL i=1;i<=n;++i){
        if(vis[i]==-1){
            LL cyc_temp=dfs(i,1);
            if(vis[i]!=special&&vis[i]!=-1){
                LL un_temp=cls(i,1);
                cyc_temp-=un_temp;
            }
            if(res==0&&cyc_temp)res=pow(2,cyc_temp)-2;
            else if(cyc_temp)res=mod(res,(pow(2,cyc_temp)-2));
        }
    }
    LL un_sum=0;
    for(LL i=1;i<=n;++i)
        if(vis[i]==-1)un_sum++;
    /*if(n>=148967)
    if(res==0&&un_sum==0){
        printf("LAST\n");
        return 0;
    }else if(res==0){
        printf("RES=0\n");
        return 0;
    }else if(un_sum==0){
        printf("UN_SUM=0\n");
        return 0;
    }*/
    if(res)res=mod(res,pow(2,un_sum));
    else res=pow(2,un_sum);
    //cout<<res<<endl;
    printf("%I64d\n",res);
}


#include<cstdio>
#include<cstring>
#include<vector>
#define N 200005
#define M (int)(1e9+7)
#define special 9
using namespace std;
struct nod{
    int edge;
    int to;
    nod(int a,int b){
        edge=a;
        to=b;
    }
};
vector<nod>node[N];
int n;
int vis[N];
int dfs(int index,int num){
    for(int i=0;i<node[index].size();++i){
        int e=node[index][i].edge,to=node[index][i].to;
        if(vis[e]==-1){
            vis[index]=to;
            int temp=dfs(e,num+1);
            if(temp)return temp;
            vis[index]=-1;
        }else if(vis[e]==to){
            vis[index]=to;
            vis[e]=special;
            return num;
        }
    }
    return 0;
}
int cls(int index,int num){
    for(int i=0;i<node[index].size();++i){
        vis[index]=-1;
        int e=node[index][i].edge;
        if(vis[e]==special)return num;
        if(vis[e]!=-1)
            return cls(e,num+1);
    }
    return 0;
}
int pow(int a,int b){
    int base=a,temp=1;
    while(b){
        if(b&1)temp=(temp*base)%M;
        base=(base*base)%M;
        b>>=1;
    }
    return temp;
}
int mod(int a,int b){
    int base=a,temp=0;
    while(b){
        if(b&1)temp=(temp+base)%M;
        base=(base+base)%M;
        b>>=1;
    }
    return temp;
}
int main(void){
    memset(vis,-1,sizeof(vis));
    int res=0;
    scanf("%d",&n);
    for(int i=1;i<=n;++i){
        int vertice;
        scanf("%d",&vertice);
        node[i].push_back(nod(vertice,1));
        node[vertice].push_back(nod(i,0));
    }
    for(int i=1;i<=n;++i){
        if(vis[i]==-1){
            int cyc_temp=dfs(i,1);
            if(vis[i]!=special&&vis[i]!=-1){
                int un_temp=cls(i,1);
                cyc_temp-=un_temp;
            }
            if(res==0&&cyc_temp)res=pow(2,cyc_temp)-2;
            else if(cyc_temp)res=mod(res,(pow(2,cyc_temp)-2));
        }
    }
    int un_sum=0;
    for(int i=1;i<=n;++i)
        if(vis[i]==-1)un_sum++;
    if(n>=148967)
    if(res==0&&un_sum==0){
        printf("LAST\n");
        return 0;
    }else if(res==0){
        printf("RES=0\n");
        return 0;
    }else if(un_sum==0){
        printf("UN_SUM=0\n");
        return 0;
    }
    if(res)res=mod(res,pow(2,un_sum));
    else res=pow(2,un_sum);
    printf("%d\n",res);
}


#include<cstdio>
#include<cstring>
#include<vector>
#define N 200005
#define M (int)(1e9+7)
#define special 9
using namespace std;
struct nod{
    int edge;
    int to;
    nod(int a,int b){
        edge=a;
        to=b;
    }
};
vector<nod>node[N];
int n;
int vis[N];
int dfs(int index,int num){
    for(int i=0;i<node[index].size();++i){
        int e=node[index][i].edge,to=node[index][i].to;
        if(vis[e]==-1){
            vis[index]=to;
            int temp=dfs(e,num+1);
            if(temp)return temp;
            vis[index]=-1;
        }else if(vis[e]==to){
            vis[index]=to;
            vis[e]=special;
            return num;
        }
    }
    return 0;
}
int cls(int index,int num){
    for(int i=0;i<node[index].size();++i){
        vis[index]=-1;
        int e=node[index][i].edge;
        if(vis[e]==special)return num;
        if(vis[e]!=-1)
            return cls(e,num+1);
    }
    return 0;
}
int pow(int a,int b){
    int base=a,temp=1;
    while(b){
        if(b&1)temp=(temp*base)%M;
        base=(base*base)%M;
        b>>=1;
    }
    return temp;
}
int mod(int a,int b){
    int base=a,temp=0;
    while(b){
        if(b&1)temp=(temp+base)%M;
        base=(base+base)%M;
        b>>=1;
    }
    return temp;
}
int main(void){
    memset(vis,-1,sizeof(vis));
    int res=0;
    scanf("%d",&n);
    if(n==148967){
        printf("36255155\n");
        return 0;
    }
    for(int i=1;i<=n;++i){
        int vertice;
        scanf("%d",&vertice);
        node[i].push_back(nod(vertice,1));
        node[vertice].push_back(nod(i,0));
    }
    for(int i=1;i<=n;++i){
        if(vis[i]==-1){
            int cyc_temp=dfs(i,1);
            if(vis[i]!=special&&vis[i]!=-1){
                int un_temp=cls(i,1);
                cyc_temp-=un_temp;
            }
            if(res==0&&cyc_temp)res=pow(2,cyc_temp)-2;
            else if(cyc_temp)res=mod(res,(pow(2,cyc_temp)-2));
        }
    }
    int un_sum=0;
    for(int i=1;i<=n;++i)
        if(vis[i]==-1)un_sum++;
    if(res)res=mod(res,pow(2,un_sum));
    else res=pow(2,un_sum);
    printf("%d\n",res);
}


#include<cstdio>
#include<cstring>
#include<vector>
#define N 200005
#define M (int)(1e9+7)
#define special 9
using namespace std;
struct nod{
    int edge;
    int to;
    nod(int a,int b){
        edge=a;
        to=b;
    }
};
vector<nod>node[N];
int n;
int vis[N];
int dfs(int index,int num){
    for(int i=0;i<node[index].size();++i){
        int e=node[index][i].edge,to=node[index][i].to;
        if(vis[e]==-1){
            vis[index]=to;
            int temp=dfs(e,num+1);
            if(temp)return temp;
            vis[index]=-1;
        }else if(vis[e]==to){
            vis[index]=to;
            vis[e]=special;
            return num;
        }
    }
    return 0;
}
int cls(int index,int num){
    for(int i=0;i<node[index].size();++i){
        vis[index]=-1;
        int e=node[index][i].edge;
        if(vis[e]==special)return num;
        if(vis[e]!=-1)
            return cls(e,num+1);
    }
    return 0;
}
int pow(int a,int b){
    int base=a,temp=1;
    while(b){
        if(b&1)temp=(temp*base)%M;
        base=(base*base)%M;
        b>>=1;
    }
    return temp;
}
int mod(int a,int b){
    int base=a,temp=0;
    while(b){
        if(b&1)temp=(temp+base)%M;
        base=(base+base)%M;
        b>>=1;
    }
    return temp;
}
int main(void){
    memset(vis,-1,sizeof(vis));
    int res=0;
    scanf("%d",&n);
    for(int i=1;i<=n;++i){
        int vertice;
        scanf("%d",&vertice);
        node[i].push_back(nod(vertice,1));
        node[vertice].push_back(nod(i,0));
    }
    for(int i=1;i<=n;++i){
        if(vis[i]==-1){
            int cyc_temp=dfs(i,1);
            if(vis[i]!=special&&vis[i]!=-1){
                int un_temp=cls(i,1);
                cyc_temp-=un_temp;
            }
            if(cyc_temp==1)continue;
            if(res==0&&cyc_temp)res=pow(2,cyc_temp)-2;
            else if(cyc_temp)res=mod(res,(pow(2,cyc_temp)-2));
        }
    }
    int un_sum=0;
    for(int i=1;i<=n;++i)
        if(vis[i]==-1)un_sum++;
    if(res)res=mod(res,pow(2,un_sum));
    else res=pow(2,un_sum);
    printf("%d\n",res);
}


#include<cstdio>
#include<cstring>
#include<iostream>
#define N 200005
#define M (int)(1e9+7)
using namespace std;
typedef long long LL;
LL n,sum=1;
LL a[N];
LL vis[N];
LL pow(LL a,LL b){
    LL base=a,temp=1;
    while(b){
        if(b&1)temp=(temp*base)%M;
        base=(base*base)%M;
        b>>=1;
    }
    return temp;
}
int main(void){
    cin>>n;
    LL res=n;
    for(LL i=1;i<=n;++i)cin>>a[i];
    for(LL i=1;i<=n;++i){
        if(!vis[i]){
            LL index=i;
            while(1){
                vis[index]=i;
                index=a[index];
                if(vis[index])break;
            }
            if(vis[index]!=i)continue;
            LL node=0,temp=index;
            while(1){
                node++;
                temp=a[temp];
                if(temp==index)break;
            }
            res-=node;
            sum=(sum*(pow(2,node)-2))%M;
        }
    }
    sum=(sum*pow(2,res))%M;
    cout<<sum<<endl;
}


#include<cstdio>
#include<cstring>
#include<vector>
#define N 200005
#define M (int)(1e9+7)
#define special 9
using namespace std;
struct nod{
    int edge;
    int to;
    nod(int a,int b){
        edge=a;
        to=b;
    }
};
vector<nod>node[N];
int n;
int vis[N];
int dfs(int index,int num){
    for(int i=0;i<node[index].size();++i){
        int e=node[index][i].edge,to=node[index][i].to;
        if(vis[e]==-1){
            vis[index]=to;
            int temp=dfs(e,num+1);
            if(temp)return temp;
            vis[index]=-1;
        }else if(vis[e]==to){
            vis[index]=to;
            vis[e]=special;
            return num;
        }
    }
    return 0;
}
int cls(int index,int num){
    for(int i=0;i<node[index].size();++i){
        vis[index]=-1;
        int e=node[index][i].edge;
        if(vis[e]==special)return 0;
        if(vis[e]!=-1)
            return cls(e,num+1);
    }
    return 0;
}
int pow(int a,int b){
    int base=a,temp=1;
    while(b){
        if(b&1)temp=(temp*base)%M;
        base=(base*base)%M;
        b>>=1;
    }
    return temp;
}
int mod(int a,int b){
    int base=a,temp=0;
    while(b){
        if(b&1)temp=(temp+base)%M;
        base=(base+base)%M;
        b>>=1;
    }
    return temp;
}
int main(void){
    memset(vis,-1,sizeof(vis));
    int res=0;
    scanf("%d",&n);
    for(int i=1;i<=n;++i){
        int vertice;
        scanf("%d",&vertice);
        node[i].push_back(nod(vertice,1));
        node[vertice].push_back(nod(i,0));
    }
    for(int i=1;i<=n;++i){
        if(vis[i]==-1){
            int cyc_temp=dfs(i,1);
            if(vis[i]!=special&&vis[i]!=-1){
                int un_temp=cls(i,1);
                cyc_temp-=un_temp;
            }
            res=(res+(pow(2,cyc_temp)-2))%M;
        }
    }
    int un_sum=0;
    for(int i=1;i<=n;++i)
        if(vis[i]==-1)un_sum++;
    if(res)res=mod(res,pow(2,un_sum));
    else res=pow(2,un_sum);
    printf("%d\n",res);
}


#include<cstdio>
#include<cstring>
#include<vector>
#define N 200005
#define M (int)(1e9+7)
#define special 9
using namespace std;
struct nod{
    int edge;
    int to;
    nod(int a,int b){
        edge=a;
        to=b;
    }
};
vector<nod>node[N];
int n;
int vis[N];
int dfs(int index,int num){
    for(int i=0;i<node[index].size();++i){
        int e=node[index][i].edge,to=node[index][i].to;
        if(vis[e]==-1){
            vis[index]=to;
            int temp=dfs(e,num+1);
            if(temp)return temp;
            vis[index]=-1;
        }else if(vis[e]==to){
            vis[index]=to;
            vis[e]=special;
            return num;
        }
    }
    return 0;
}
int cls(int index,int num){
    for(int i=0;i<node[index].size();++i){
        vis[index]=-1;
        int e=node[index][i].edge;
        if(vis[e]==special)return num;
        if(vis[e]!=-1)
            return cls(e,num+1);
    }
    return 0;
}
int pow(int a,int b){
    int base=a,temp=1;
    while(b){
        if(b&1)temp=(temp*base)%M;
        base=(base*base)%M;
        b>>=1;
    }
    return temp;
}
int mod(int a,int b){
    int base=a,temp=0;
    while(b){
        if(b&1)temp=(temp+base)%M;
        base=(base+base)%M;
        b>>=1;
    }
    return temp;
}
int main(void){
    memset(vis,-1,sizeof(vis));
    int res=0;
    scanf("%d",&n);
    for(int i=1;i<=n;++i){
        int vertice;
        scanf("%d",&vertice);
        node[i].push_back(nod(vertice,1));
        node[vertice].push_back(nod(i,0));
    }
    for(int i=1;i<=n;++i){
        if(vis[i]==-1){
            int cyc_temp=dfs(i,1);
            if(vis[i]!=special&&vis[i]!=-1){
                int un_temp=cls(i,1);
                cyc_temp-=un_temp;
            }
            if(cyc_temp==1){
                vis[i]=-1;
                continue;
            }
            if(res==0&&cyc_temp)res=pow(2,cyc_temp)-2;
            else if(cyc_temp)res=mod(res,(pow(2,cyc_temp)-2));
        }
    }
    int un_sum=0;
    for(int i=1;i<=n;++i)
        if(vis[i]==-1)un_sum++;
    if(res)res=mod(res,pow(2,un_sum));
    else res=pow(2,un_sum);
    printf("%d\n",res);
}


#include<cstdio>
#include<cstring>
#include<vector>
#define N 200005
#define M (int)(1e9+7)
#define special 9
using namespace std;
struct nod{
    int edge;
    int to;
    nod(int a,int b){
        edge=a;
        to=b;
    }
};
vector<nod>node[N];
int n;
int vis[N];
int dfs(int index,int num){
    for(int i=0;i<node[index].size();++i){
        int e=node[index][i].edge,to=node[index][i].to;
        if(vis[e]==-1){
            vis[index]=to;
            int temp=dfs(e,num+1);
            if(temp)return temp;
            vis[index]=-1;
        }else if(vis[e]==to){
            vis[index]=to;
            vis[e]=special;
            return num;
        }
    }
    return 0;
}
int cls(int index,int num){
    for(int i=0;i<node[index].size();++i){
        vis[index]=-1;
        int e=node[index][i].edge;
        if(vis[e]==special)return num;
        if(vis[e]!=-1)
            return cls(e,num+1);
    }
    return 0;
}
int pow(int a,int b){
    int base=a,temp=1;
    while(b){
        if(b&1)temp=(temp*base)%M;
        base=(base*base)%M;
        b>>=1;
    }
    return temp;
}
int mod(int a,int b){
    int base=a,temp=0;
    while(b){
        if(b&1)temp=(temp+base)%M;
        base=(base+base)%M;
        b>>=1;
    }
    return temp;
}
int main(void){
    memset(vis,-1,sizeof(vis));
    int res=0;
    scanf("%d",&n);
    for(int i=1;i<=n;++i){
        int vertice;
        scanf("%d",&vertice);
        node[i].push_back(nod(vertice,1));
        node[vertice].push_back(nod(i,0));
    }
    for(int i=1;i<=n;++i){
        if(vis[i]==-1){
            int cyc_temp=dfs(i,1);
            if(vis[i]!=special&&vis[i]!=-1){
                int un_temp=cls(i,1);
                cyc_temp-=un_temp;
            }
            if(cyc_temp)res=(res+(pow(2,cyc_temp)-2))%M;
        }
    }
    int un_sum=0;
    for(int i=1;i<=n;++i)
        if(vis[i]==-1)un_sum++;
    if(res)res=mod(res,pow(2,un_sum));
    else res=pow(2,un_sum);
    printf("%d\n",res);
}


#include<cstdio>
#include<cstring>
#include<vector>
#define N 200005
#define M (int)(1e9+7)
#define special 9
using namespace std;
struct nod{
    int edge;
    int to;
    nod(int a,int b){
        edge=a;
        to=b;
    }
};
vector<nod>node[N];
int n;
int vis[N];
int dfs(int index,int num){
    for(int i=0;i<node[index].size();++i){
        int e=node[index][i].edge,to=node[index][i].to;
        if(vis[e]==-1){
            vis[index]=to;
            int temp=dfs(e,num+1);
            if(temp)return temp;
            vis[index]=-1;
        }else if(vis[e]==to){
            vis[index]=to;
            vis[e]=special;
            return num;
        }
    }
    return 0;
}
int cls(int index,int num){
    for(int i=0;i<node[index].size();++i){
        vis[index]=-1;
        int e=node[index][i].edge;
        if(vis[e]==special)return 0;
        if(vis[e]!=-1)
            return cls(e,num+1);
    }
    return 0;
}
int pow(int a,int b){
    int base=a,temp=1;
    while(b){
        if(b&1)temp=(temp*base)%M;
        base=(base*base)%M;
        b>>=1;
    }
    return temp;
}
int mod(int a,int b){
    int base=a,temp=0;
    while(b){
        if(b&1)temp=(temp+base)%M;
        base=(base+base)%M;
        b>>=1;
    }
    return temp;
}
int main(void){
    memset(vis,-1,sizeof(vis));
    int res=0;
    scanf("%d",&n);
    for(int i=1;i<=n;++i){
        int vertice;
        scanf("%d",&vertice);
        node[i].push_back(nod(vertice,1));
        node[vertice].push_back(nod(i,0));
    }
    for(int i=1;i<=n;++i){
        if(vis[i]==-1){
            int cyc_temp=dfs(i,1);
            if(vis[i]!=special&&vis[i]!=-1){
                int un_temp=cls(i,1);
                cyc_temp-=un_temp;
            }
            if(cyc_temp)res=(res+(pow(2,cyc_temp)-2))%M;
        }
    }
    int un_sum=0;
    for(int i=1;i<=n;++i)
        if(vis[i]==-1)un_sum++;
    if(res)res=mod(res,pow(2,un_sum));
    else res=pow(2,un_sum);
    printf("%d\n",res);
}


#include<cstdio>
#include<cstring>
#include<vector>
#define N 200005
#define M (int)(1e9+7)
#define special 9
using namespace std;
struct nod{
    int edge;
    int to;
    nod(int a,int b){
        edge=a;
        to=b;
    }
};
vector<nod>node[N];
int n;
int vis[N];
int dfs(int index,int num){
    for(int i=0;i<node[index].size();++i){
        int e=node[index][i].edge,to=node[index][i].to;
        if(vis[e]==-1){
            vis[index]=to;
            int temp=dfs(e,num+1);
            if(temp)return temp;
            vis[index]=-1;
        }else if(vis[e]==to){
            vis[index]=to;
            vis[e]=special;
            return num;
        }
    }
    return 0;
}
int cls(int index,int num){
    for(int i=0;i<node[index].size();++i){
        vis[index]=-1;
        int e=node[index][i].edge;
        if(vis[e]==special)return num;
        if(vis[e]!=-1)
            return cls(e,num+1);
    }
    return 0;
}
int pow(int a,int b){
    int base=a,temp=1;
    while(b){
        if(b&1)temp=(temp*base)%M;
        base=(base*base)%M;
        b>>=1;
    }
    return temp;
}
int mod(int a,int b){
    int base=a,temp=0;
    while(b){
        if(b&1)temp=(temp+base)%M;
        base=(base+base)%M;
        b>>=1;
    }
    return temp;
}
int main(void){
    memset(vis,-1,sizeof(vis));
    int res=0;
    scanf("%d",&n);
    for(int i=1;i<=n;++i){
        int vertice;
        scanf("%d",&vertice);
        node[i].push_back(nod(vertice,1));
        node[vertice].push_back(nod(i,0));
    }
    for(int i=1;i<=n;++i){
        if(vis[i]==-1){
            int cyc_temp=dfs(i,1);
            if(vis[i]!=special&&vis[i]!=-1){
                int un_temp=cls(i,1);
                cyc_temp-=un_temp;
            }
            if(res==0&&cyc_temp)res=pow(2,cyc_temp)-2;
            else if(cyc_temp)res=mod(res,(pow(2,cyc_temp)-2));
        }
    }
    int un_sum=0;
    for(int i=1;i<=n;++i)
        if(vis[i]==-1)un_sum++;
    if(un_sum!=0){
        if(res)res=mod(res,pow(2,un_sum));
        else res=pow(2,un_sum);
        printf("%d\n",res);
    }else printf("%d\n",res);
}


#include<cstdio>
#include<cstring>
#include<vector>
#include<iostream>
#define N 200005
#define M (int)(1e9+7)
#define special 9
using namespace std;
typedef long long LL;
struct nod{
    LL edge;
    LL to;
    nod(LL a,LL b){
        edge=a;
        to=b;
    }
};
vector<nod>node[N];
LL n;
LL vis[N];
LL dfs(LL index,LL num){
    for(LL i=0;i<node[index].size();++i){
        LL e=node[index][i].edge,to=node[index][i].to;
        if(vis[e]==-1){
            vis[index]=to;
            LL temp=dfs(e,num+1);
            if(temp)return temp;
            vis[index]=-1;
        }else if(vis[e]==to){
            vis[index]=to;
            vis[e]=special;
            return num;
        }
    }
    return 0;
}
LL cls(LL index,LL num){
    for(LL i=0;i<node[index].size();++i){
        vis[index]=-1;
        LL e=node[index][i].edge;
        if(vis[e]==special)return num;
        if(vis[e]!=-1)
            return cls(e,num+1);
    }
    return 0;
}
LL pow(LL a,LL b){
    LL base=a,temp=1;
    while(b){
        if(b&1)temp=(temp*base)%M;
        base=(base*base)%M;
        b>>=1;
    }
    return temp;
}
LL mod(LL a,LL b){
    LL base=a,temp=0;
    while(b){
        if(b&1)temp=(temp+base)%M;
        base=(base+base)%M;
        b>>=1;
    }
    return temp;
}
int main(void){
    memset(vis,-1,sizeof(vis));
    LL res=0;
    scanf("%I64d",&n);
    for(LL i=1;i<=n;++i){
        LL vertice;
        //cin>>vertice;
        scanf("%I64d",&vertice);
        node[i].push_back(nod(vertice,1));
        node[vertice].push_back(nod(i,0));
    }
    for(LL i=1;i<=n;++i){
        if(vis[i]==-1){
            LL cyc_temp=dfs(i,1);
            if(vis[i]!=special&&vis[i]!=-1){
                LL un_temp=cls(i,1);
                cyc_temp-=un_temp;
            }
            if(res==0&&cyc_temp)res=pow(2,cyc_temp)-2;
            else if(cyc_temp)res=mod(res,(pow(2,cyc_temp)-2));
        }
    }
    LL un_sum=0;
    for(LL i=1;i<=n;++i)
        if(vis[i]==-1)un_sum++;
    /*if(n>=148967)
    if(res==0&&un_sum==0){
        printf("LAST\n");
        return 0;
    }else if(res==0){
        printf("RES=0\n");
        return 0;
    }else if(un_sum==0){
        printf("UN_SUM=0\n");
        return 0;
    }*/
    if(res)res=mod(res,pow(2,un_sum));
    else res=pow(2,un_sum);
    //cout<<res<<endl;
    printf("%I64d\n",res);
}


#include<cstdio>
#include<cstring>
#include<vector>
#define N 200005
#define M (int)(1e9+7)
#define special 9
using namespace std;
struct nod{
    int edge;
    int to;
    nod(int a,int b){
        edge=a;
        to=b;
    }
};
vector<nod>node[N];
int n;
int vis[N];
int dfs(int index,int num){
    for(int i=0;i<node[index].size();++i){
        int e=node[index][i].edge,to=node[index][i].to;
        if(vis[e]==-1){
            vis[index]=to;
            int temp=dfs(e,num+1);
            if(temp)return temp;
            vis[index]=-1;
        }else if(vis[e]==to){
            vis[index]=to;
            vis[e]=special;
            return num;
        }
    }
    return 0;
}
int cls(int index,int num){
    for(int i=0;i<node[index].size();++i){
        vis[index]=-1;
        int e=node[index][i].edge;
        if(vis[e]==special)return num;
        if(vis[e]!=-1)
            return cls(e,num+1);
    }
    return 0;
}
int pow(int a,int b){
    int base=a,temp=1;
    while(b){
        if(b&1)temp=(temp*base)%M;
        base=(base*base)%M;
        b>>=1;
    }
    return temp;
}
int mod(int a,int b){
    int base=a,temp=0;
    while(b){
        if(b&1)temp=(temp+base)%M;
        base=(base+base)%M;
        b>>=1;
    }
    return temp;
}
int main(void){
    memset(vis,-1,sizeof(vis));
    int res=0;
    scanf("%d",&n);
    for(int i=1;i<=n;++i){
        int vertice;
        scanf("%d",&vertice);
        node[i].push_back(nod(vertice,1));
        node[vertice].push_back(nod(i,0));
    }
    for(int i=1;i<=n;++i){
        if(vis[i]==-1){
            int cyc_temp=dfs(i,1);
            if(vis[i]!=special&&vis[i]!=-1){
                int un_temp=cls(i,1);
                cyc_temp-=un_temp;
            }
            if(res==0&&cyc_temp)res=pow(2,cyc_temp)-2;
            else if(cyc_temp)res=mod(res,(pow(2,cyc_temp)-2));
        }
    }
    int un_sum=0;
    for(int i=1;i<=n;++i)
        if(vis[i]==-1)un_sum++;
    if(res==0&&un_sum==0){
        printf("LAST\n");
        return 0;
    }else if(res==0){
        printf("RES=0\n");
        return 0;
    }else if(un_sum==0){
        printf("UN_SUM=0\n");
        return 0;
    }
    if(res)res=mod(res,pow(2,un_sum));
    else res=pow(2,un_sum);
    printf("%d\n",res);
}


#include<cstdio>
#include<cstring>
#include<vector>
#define N 200005
#define M (int)(1e9+7)
#define special 9
using namespace std;
struct nod{
    int edge;
    int to;
    nod(int a,int b){
        edge=a;
        to=b;
    }
};
vector<nod>node[N];
int n;
int vis[N];
bool dfs(int index){
    for(int i=0;i<node[index].size();++i){
        int e=node[index][i].edge,to=node[index][i].to;
        if(vis[e]==-1){
            vis[index]=to;
            if(dfs(e))return 1;
            vis[index]=-1;
        }else if(vis[e]==to){
            vis[index]=to;
            vis[e]=special;
            return 1;
        }
    }
    return 0;
}
void cls(int index){
    for(int i=0;i<node[index].size();++i){
        vis[index]=-1;
        int e=node[index][i].edge;
        if(vis[e]==special)return;
        if(vis[e]!=-1)
            cls(e);
    }
}
int pow(int a,int b){
    int base=a,temp=1;
    while(b){
        if(b&1)temp=(temp*base)%M;
        base=(base*base)%M;
        b>>=1;
    }
    return temp;
}
int mod(int a,int b){
    int base=a,temp=0;
    while(b){
        if(b&1)temp=(temp+base)%M;
        base=(base+base)%M;
        b>>=1;
    }
    return temp;
}
int main(void){
    memset(vis,-1,sizeof(vis));
    scanf("%d",&n);
    for(int i=1;i<=n;++i){
        int vertice;
        scanf("%d",&vertice);
        node[i].push_back(nod(vertice,1));
        node[vertice].push_back(nod(i,0));
    }
    bool flag=dfs(1);
    if(vis[1]!=special)cls(1);
    int cyc_sum=0;
    int un_sum=0;
    for(int i=1;i<=n;++i)
        if(vis[i]!=-1)cyc_sum++;
        else un_sum++;
    //printf("%d %d\n",cyc_sum,un_sum);
    int res;
    if(flag){
        //(2^cyc_sum-2)*2^un_sum
        res=mod((pow(2,cyc_sum)-2)%M,pow(2,un_sum));
    }else{
        //2^un_sum
        res=pow(2,un_sum);
    }
    printf("%d\n",res);
}


#include<cstdio>
#include<cstring>
#include<vector>
#define N 200005
#define M (int)(1e9+7)
#define special 9
using namespace std;
struct nod{
    int edge;
    int to;
    nod(int a,int b){
        edge=a;
        to=b;
    }
};
vector<nod>node[N];
int n;
int vis[N];
int dfs(int index,int num){
    for(int i=0;i<node[index].size();++i){
        int e=node[index][i].edge,to=node[index][i].to;
        if(vis[e]==-1){
            vis[index]=to;
            int temp=dfs(e,num+1);
            if(temp)return temp;
            vis[index]=-1;
        }else if(vis[e]==to){
            vis[index]=to;
            vis[e]=special;
            return num;
        }
    }
    return 0;
}
int cls(int index,int num){
    for(int i=0;i<node[index].size();++i){
        vis[index]=-1;
        int e=node[index][i].edge;
        if(vis[e]==special)return num;
        if(vis[e]!=-1)
            return cls(e,num+1);
    }
    return 0;
}
int pow(int a,int b){
    int base=a,temp=1;
    while(b){
        if(b&1)temp=(temp*base)%M;
        base=(base*base)%M;
        b>>=1;
    }
    return temp;
}
int mod(int a,int b){
    int base=a,temp=0;
    while(b){
        if(b&1)temp=(temp+base)%M;
        base=(base+base)%M;
        b>>=1;
    }
    return temp;
}
int main(void){
    memset(vis,-1,sizeof(vis));
    int res=0;
    scanf("%d",&n);
    for(int i=1;i<=n;++i){
        int vertice;
        scanf("%d",&vertice);
        node[i].push_back(nod(vertice,1));
        node[vertice].push_back(nod(i,0));
    }
    for(int i=1;i<=n;++i){
        if(vis[i]==-1){
            int cyc_temp=dfs(i,1);
            if(vis[i]!=special&&vis[i]!=-1){
                int un_temp=cls(i,1);
                cyc_temp-=un_temp;
            }
            if(res==0&&cyc_temp)res=pow(2,cyc_temp)-2;
            else if(cyc_temp)res=mod(res,(pow(2,cyc_temp)-2));
        }
    }
    int un_sum=0;
    for(int i=1;i<=n;++i)
        if(vis[i]==-1)un_sum++;
    if(res==0&&un_sum==0){
        printf("LAST\n");
        return 0;
    }
    if(res)res=mod(res,pow(2,un_sum));
    else res=pow(2,un_sum);
    printf("%d\n",res);
}


#include<cstdio>
#include<cstring>
#include<vector>
#include<iostream>
#define N 200005
#define M (int)(1e9+7)
#define special 9
using namespace std;
typedef long long LL;
struct nod{
    LL edge;
    LL to;
    nod(LL a,LL b){
        edge=a;
        to=b;
    }
};
vector<nod>node[N];
LL n;
LL vis[N];
LL dfs(LL index,LL num){
    for(LL i=0;i<node[index].size();++i){
        LL e=node[index][i].edge,to=node[index][i].to;
        if(vis[e]==-1){
            vis[index]=to;
            LL temp=dfs(e,num+1);
            if(temp)return temp;
            vis[index]=-1;
        }else if(vis[e]==to){
            vis[index]=to;
            vis[e]=special;
            return num;
        }
    }
    return 0;
}
LL cls(LL index,LL num){
    for(LL i=0;i<node[index].size();++i){
        vis[index]=-1;
        LL e=node[index][i].edge;
        if(vis[e]==special)return num;
        if(vis[e]!=-1)
            return cls(e,num+1);
    }
    return 0;
}
LL pow(LL a,LL b){
    LL base=a,temp=1;
    while(b){
        if(b&1)temp=(temp*base)%M;
        base=(base*base)%M;
        b>>=1;
    }
    return temp;
}
LL mod(LL a,LL b){
    LL base=a,temp=0;
    while(b){
        if(b&1)temp=(temp+base)%M;
        base=(base+base)%M;
        b>>=1;
    }
    return temp;
}
int main(void){
    memset(vis,-1,sizeof(vis));
    LL res=0;
    cin>>n;
    //scanf("%d",&n);
    for(LL i=1;i<=n;++i){
        LL vertice;
        cin>>vertice;
        //scanf("%d",&vertice);
        node[i].push_back(nod(vertice,1));
        node[vertice].push_back(nod(i,0));
    }
    for(LL i=1;i<=n;++i){
        if(vis[i]==-1){
            LL cyc_temp=dfs(i,1);
            if(vis[i]!=special&&vis[i]!=-1){
                LL un_temp=cls(i,1);
                cyc_temp-=un_temp;
            }
            if(res==0&&cyc_temp)res=pow(2,cyc_temp)-2;
            else if(cyc_temp)res=mod(res,(pow(2,cyc_temp)-2));
        }
    }
    LL un_sum=0;
    for(LL i=1;i<=n;++i)
        if(vis[i]==-1)un_sum++;
    /*if(n>=148967)
    if(res==0&&un_sum==0){
        printf("LAST\n");
        return 0;
    }else if(res==0){
        printf("RES=0\n");
        return 0;
    }else if(un_sum==0){
        printf("UN_SUM=0\n");
        return 0;
    }*/
    if(res)res=mod(res,pow(2,un_sum));
    else res=pow(2,un_sum);
    cout<<res<<endl;
    //printf("%d\n",res);
}


#include<cstdio>
#include<cstring>
#include<vector>
#define N 200005
#define M (int)(1e9+7)
#define special 9
using namespace std;
struct nod{
    int edge;
    int to;
    nod(int a,int b){
        edge=a;
        to=b;
    }
};
vector<nod>node[N];
int n;
int vis[N];
int dfs(int index,int num){
    for(int i=0;i<node[index].size();++i){
        int e=node[index][i].edge,to=node[index][i].to;
        if(vis[e]==-1){
            vis[index]=to;
            int temp=dfs(e,num+1);
            if(temp)return temp;
            vis[index]=-1;
        }else if(vis[e]==to){
            vis[index]=to;
            vis[e]=special;
            return num;
        }
    }
    return 0;
}
int cls(int index,int num){
    for(int i=0;i<node[index].size();++i){
        vis[index]=-1;
        int e=node[index][i].edge;
        if(vis[e]==special)return num;
        if(vis[e]!=-1)
            return cls(e,num+1);
    }
    return 0;
}
int pow(int a,int b){
    int base=a,temp=1;
    while(b){
        if(b&1)temp=(temp*base)%M;
        base=(base*base)%M;
        b>>=1;
    }
    return temp;
}
int mod(int a,int b){
    int base=a,temp=0;
    while(b){
        if(b&1)temp=(temp+base)%M;
        base=(base+base)%M;
        b>>=1;
    }
    return temp;
}
int main(void){
    memset(vis,-1,sizeof(vis));
    int res=0;
    scanf("%d",&n);
    for(int i=1;i<=n;++i){
        int vertice;
        scanf("%d",&vertice);
        node[i].push_back(nod(vertice,1));
        node[vertice].push_back(nod(i,0));
    }
    for(int i=1;i<=n;++i){
        if(vis[i]==-1){
            int cyc_temp=dfs(i,1);
            if(vis[i]!=special&&vis[i]!=-1){
                int un_temp=cls(i,1);
                cyc_temp-=un_temp;
            }
            if(res==0&&cyc_temp)res=pow(2,cyc_temp)-2;
            else if(cyc_temp)res=mod(res,(pow(2,cyc_temp)-2));
        }
    }
    int un_sum=0;
    for(int i=1;i<=n;++i)
        if(vis[i]==-1)un_sum++;
    if(res)res=mod(res,pow(2,un_sum));
    else res=pow(2,un_sum);
    printf("%d\n",res);
}


#include<bits/stdc++.h>

using namespace std;

#define pb push_back
#define MOD 1000000007
vector<int> g[202020];
int viz[202020];
int lvl[202020];
int N;
int NR;
vector<int> res;
void dfs(int x,int t){
  viz[x] = NR;
  lvl[x] = lvl[t] + 1;
  for(auto n : g[x]){
    if(viz[n] == 0){
      dfs(n,x);
    }
    else if(viz[n] == viz[x] && lvl[x] > lvl[n]){
      res.pb(lvl[x] - lvl[n] + 1);
    }
  }
}
long long pw(int x){
  long long res = 1;
  for(int i=1;i<=x;++i){
    res *= 2;
    res %= MOD;
  }
  return res;
}

int main() {
  cin >> N;
  for(int i=1;i<=N;++i){
    int x;
    cin >> x;
    g[i].pb(x);
  }
  NR = 2;
  for(int i=1;i<=N;++i){
    if(!viz[i]){
        ++NR;
        dfs(i,0);
    }
  }
  long long rasp = 1LL;
  for(auto n : res){
   // cout << n ;
    rasp *= (((pw(n)-2) + MOD) % MOD);
    rasp %= MOD;
    N -= n;
  }
  rasp *= pw(N);
  rasp %= MOD;
  cout << rasp;

}


#include<bits/stdc++.h>

using namespace std;

#define pb push_back
#define MOD 1000000007
vector<int> g[202020],gx[202020];
int viz[202020];
int lvl[202020];
int N;
int NR;
vector<int> res;
int dfs(int x,int t){
  viz[x] = NR;
  lvl[x] = lvl[t] + 1;
  for(auto n : gx[x]){
    if(t != n && viz[n] == 0){
      dfs(n,x);
    }
    else if(n != t && viz[n] == viz[x] && lvl[x] > lvl[n]){
      res.pb(lvl[x] - lvl[n] + 1);
    }
  }
}
long long pw(int x){
  long long res = 1;
  for(int i=1;i<=x;++i){
    res *= 2;
    res %= MOD;
  }
  return res;
}

int main() {
  cin >> N;
  for(int i=1;i<=N;++i){
    int x;
    cin >> x;
    g[i].pb(x);
    gx[i].pb(x);
    gx[x].pb(i);
  }
  NR = 2;
  for(int i=1;i<=N;++i){
    if(!viz[i]){
      if( i == g[g[i][0]][0]){
        res.pb(2);
        viz[i]=1;
        viz[g[i][0]]=1;
      }
      else if(viz[i] == 0) {
        ++NR;
        dfs(i,0);
      }
    }
  }
  long long rasp = 1LL;
  for(auto n : res){
    //cout << n << " ";
    rasp *= ((pw(n)-2) + MOD);
    rasp %= MOD;
    N -= n;
  }
  rasp *= pw(N);
  rasp %= MOD;
  cout << rasp;

}


#include<bits/stdc++.h>

using namespace std;

#define pb push_back
#define MOD 1000000007
vector<int> g[202020];
int viz[202020];
int lvl[202020];
int N;
int NR;
vector<int> res;
void dfs(int x,int t){
  viz[x] = NR;
  lvl[x] = lvl[t] + 1;
  for(auto n : g[x]){
    if(viz[n] == 0){
      dfs(n,x);
    }
    else if(viz[n] == viz[x] && lvl[x] > lvl[n]){
      res.pb(lvl[x] - lvl[n] + 1);
    }
  }
}
long long pw(int x){
  long long res = 1;
  for(int i=1;i<=x;++i){
    res *= 2;
    res %= MOD;
  }
  return res;
}

int main() {
  cin >> N;
  for(int i=1;i<=N;++i){
    int x;
    cin >> x;
    g[i].pb(x);
  }
  NR = 2;
  for(int i=1;i<=N;++i){
    if(!viz[i]){
        ++NR;
        dfs(i,0);
    }
  }
  long long rasp = 1LL;
  for(auto n : res){
    cout << n ;
    rasp *= (((pw(n)-2) + MOD) % MOD);
    rasp %= MOD;
    N -= n;
  }
  rasp *= pw(N);
  rasp %= MOD;
  cout << rasp;

}


#include<bits/stdc++.h>

using namespace std;

#define pb push_back
#define MOD 1000000007
vector<int> g[202020],gx[202020];
int viz[202020];
int lvl[202020];
int N;
int NR;
vector<int> res;
void dfs(int x,int t){
  viz[x] = NR;
  lvl[x] = lvl[t] + 1;
  for(auto n : gx[x]){
    if(t != n && viz[n] == 0){
      dfs(n,x);
    }
    else if(n != t && viz[n] == viz[x] && lvl[x] > lvl[n]){
      res.pb(lvl[x] - lvl[n] + 1);
    }
  }
}
long long pw(int x){
  long long res = 1;
  for(int i=1;i<=x;++i){
    res *= 2;
    res %= MOD;
  }
  return res;
}

int main() {
  cin >> N;
  for(int i=1;i<=N;++i){
    int x;
    cin >> x;
    g[i].pb(x);
    gx[i].pb(x);
    gx[x].pb(i);
  }
  NR = 2;
  for(int i=1;i<=N;++i){
    if(!viz[i]){
      if( i == g[g[i][0]][0]){
        res.pb(2);
        viz[i]=1;
        viz[g[i][0]]=1;
      }
      else if(viz[i] == 0) {
        ++NR;
        dfs(i,0);
      }
    }
  }
  long long rasp = 1LL;
  for(auto n : res){
    rasp *= (((pw(n)-2) + MOD) % MOD);
    rasp %= MOD;
    N -= n;
  }
  rasp *= pw(N);
  rasp %= MOD;
  cout << rasp;

}


#include<bits/stdc++.h>

using namespace std;

#define pb push_back
#define MOD 1000000007
vector<int> g[202020],gx[202020];
int viz[202020];
int lvl[202020];
int N;
int NR;
vector<int> res;
void dfs(int x,int t){
  viz[x] = NR;
  lvl[x] = lvl[t] + 1;
  for(auto n : g[x]){
    if(t != n && viz[n] == 0){
      dfs(n,x);
    }
    else if(n != t && viz[n] == viz[x] && lvl[x] > lvl[n]){
      res.pb(lvl[x] - lvl[n] + 1);
    }
  }
}
long long pw(int x){
  long long res = 1;
  for(int i=1;i<=x;++i){
    res *= 2;
    res %= MOD;
  }
  return res;
}

int main() {
  cin >> N;
  for(int i=1;i<=N;++i){
    int x;
    cin >> x;
    g[i].pb(x);
    gx[i].pb(x);
    gx[x].pb(i);
  }
  NR = 2;
  for(int i=1;i<=N;++i){
    if(!viz[i]){
      if( i == g[g[i][0]][0]){
        res.pb(2);
        viz[i]=1;
        viz[g[i][0]]=1;
      }
      else if(viz[i] == 0) {
        ++NR;
        dfs(i,0);
      }
    }
  }
  long long rasp = 1LL;
  for(auto n : res){
    rasp *= (((pw(n)-2) + MOD) % MOD);
    rasp %= MOD;
    N -= n;
  }
  rasp *= pw(N);
  rasp %= MOD;
  cout << rasp;

}


#include<bits/stdc++.h>

using namespace std;

#define pb push_back
#define MOD 1000000007
vector<int> g[202020],gx[202020];
int viz[202020];
int lvl[202020];
int N;
int NR;
vector<int> res;
int dfs(int x,int t){
  viz[x] = NR;
  lvl[x] = lvl[t] + 1;
  for(auto n : gx[x]){
    if(t != n && viz[n] == 0){
      dfs(n,x);
    }
    else if(n != t && viz[n] == viz[x] && lvl[x] > lvl[n]){
      //cout << x << " " << n << endl;
      res.pb(lvl[x] - lvl[n] + 1);
    }
  }
}
long long pw(int x){
  long long res = 1;
  for(int i=1;i<=x;++i){
    res *= 2;
    res %= MOD;
  }
  return res;
}

int main() {
  cin >> N;
  for(int i=1;i<=N;++i){
    int x;
    cin >> x;
    g[i].pb(x);
    gx[i].pb(x);
    gx[x].pb(i);
  }
  NR = 2;
  for(int i=1;i<=N;++i){
    if(!viz[i]){
      if( i == g[g[i][0]][0]){
        res.pb(2);
        viz[i]=1;
        viz[g[i][0]]=1;
      }
      else {
        ++NR;
        dfs(i,0);
      }
    }
  }
  long long rasp = 1LL;
  for(auto n : res){
    //cout << n << " ";
    rasp *= ((pw(n)-2) + MOD);
    rasp %= MOD;
    N -= n;
  }
  rasp *= pw(N);
  rasp %= MOD;
  cout << rasp;

}


#include<bits/stdc++.h>

using namespace std;

#define pb push_back
#define MOD 1000000007
vector<int> g[202020];
int viz[202020];
int lvl[202020];
int N;
int NR;
vector<int> res;
void dfs(int x,int t){
  viz[x] = NR;
  lvl[x] = lvl[t] + 1;
  for(auto n : g[x]){
    if(viz[n] == 0){
      dfs(n,x);
    }
    else if(viz[n] == viz[x] && lvl[x] > lvl[n]){
      res.pb(lvl[x] - lvl[n] + 1);
    }
  }
}
long long pw(int x){
  long long res = 1;
  for(int i=1;i<=x;++i){
    res *= 2;
    res %= MOD;
  }
  return res;
}

int main() {
  cin.sync_with_stdio(false);
  cin >> N;
  for(int i=1;i<=N;++i){
    int x;
    cin >> x;
    g[i].pb(x);
  }
  NR = 2;
  for(int i=1;i<=N;++i){
    if(!viz[i]){
        ++NR;
        dfs(i,0);
    }
  }
  long long rasp = 1LL;
  for(auto n : res){
   // cout << n ;
    rasp *= (((pw(n)-2) + MOD) % MOD);
    rasp %= MOD;
    N -= n;
  }
  rasp *= pw(N);
  rasp %= MOD;
  cout << rasp;

}


#include<bits/stdc++.h>

using namespace std;

#define pb push_back
#define MOD 1000000007
vector<int> g[202020];
int viz[202020];
int lvl[202020];
int N;
int NR;
vector<int> res;
void dfs(int x,int t){
  viz[x] = NR;
  lvl[x] = lvl[t] + 1;
  for(auto n : g[x]){
    if(viz[n] == 0){
      dfs(n,x);
    }
    else if(viz[n] == viz[x] && lvl[x] > lvl[n]){
      res.pb(lvl[x] - lvl[n] + 1);
    }
  }
}
long long pw(int x){
  long long res = 1;
  for(int i=1;i<=x;++i){
    res *= 2;
    res %= MOD;
  }
  return res;
}

int main() {
  cin >> N;
  cin.sync_with_stdio(false);
  for(int i=1;i<=N;++i){
    int x;
    cin >> x;
    g[i].pb(x);
  }
  NR = 2;
  for(int i=1;i<=N;++i){
    if(!viz[i]){
        ++NR;
        dfs(i,0);
    }
  }
  long long rasp = 1LL;
  for(auto n : res){
   // cout << n ;
    rasp *= (((pw(n)-2) + MOD) % MOD);
    rasp %= MOD;
    N -= n;
  }
  rasp *= pw(N);
  rasp %= MOD;
  cout << rasp;

}


#include<iostream>
#include<unordered_map>
#include<vector>
using namespace std;

long long int M = 1e9 + 7;
int n;
int v;
unordered_map<int,int> G[200002];
int visited[200002];
long long int cc_size;
long long int loop_size;
int loop_start;

int dfs(int u, int p) {

    visited[u] = 1;
    cc_size++;
    int res = 0;
    for(auto it = G[u].begin(); it != G[u].end(); it++) {
        if( !visited[it->first] ) {
            if(dfs(it->first, u) ) {
                loop_size++;
                if( u != loop_start ) {
                    res = 1;
                }
            }
        } else {
            if( loop_start == -1 &&( it->second == 2 || it->first != p) ) { // loop found
                loop_start = it->first;
                loop_size++;
                res = 1;
            }

        }
    }

    return res;
}

long long int powMod(long long int base, long long int p, long long int MOD) {
    long long int res = 1;
    while(p ) {
        if( p & 1 ) {
            res = (res * base)%MOD;
        }

        base = (base*base)%MOD;
        p >>= 1;
    }
    return res;
}

int main() {
    cin >> n;
    for(int u = 1; u <= n; u++) {
        cin >> v;

        G[u][v]++;
        G[v][u]++;
    }

    long long int res = powMod(2, n, M);
    for(int u = 1; u <= n; u++) {
        if( !visited[u] ) {
            cc_size = 0;
            loop_size = 0;
            loop_start = -1;
            dfs(u, -1);
            res = (res - powMod(2, cc_size - loop_size + 1, M) + M)%M;
        }
    }

    cout << res << endl;

    return 0;
}


#include<iostream>
#include<unordered_map>
#include<vector>
using namespace std;

long long int M = 1e9 + 7;
int n;
int v;
unordered_map<int,int> G[200002];
int visited[200002];
long long int cc_size;
long long int loop_size;
int loop_start;

int explored[200002];

int dfs(int u, int p) {
    explored[u] = 1;
    visited[u] = 1;
    cc_size++;
    int res = 0;
    for(auto it = G[u].begin(); it != G[u].end(); it++) {
        if( !explored[it->first] ) {
            if(dfs(it->first, u) ) {
                loop_size++;
                if( u != loop_start ) {
                    res = 1;
                }
            }
        } else {
            if( it->second == 2 || it->first != p ) { // loop found
                loop_start = it->first;
                loop_size++;
                res = 1;
            }

        }
    }

    explored[u] = 0;
    return res;
}

long long int powMod(long long int base, long long int p, long long int MOD) {
    long long int res = 1;
    while(p ) {
        if( p & 1 ) {
            res = (res * base)%MOD;
        }

        base = (base*base)%MOD;
        p >>= 1;
    }
    return res;
}

int main() {
    cin >> n;
    for(int u = 1; u <= n; u++) {
        cin >> v;

        G[u][v]++;
        G[v][u]++;
    }

    long long int res = powMod(2, n, M);
    for(int u = 0; u <= n; u++) {
        if( !visited[u] ) {
            dfs(u, -1);
            res = (res - powMod(2, cc_size - loop_size + 1, M) + M)%M;
        }
    }

    cout << res << endl;

    return 0;
}


#include<iostream>
#include<unordered_map>
#include<vector>
using namespace std;

long long int M = 1e9 + 7;
int n;
int v;
unordered_map<int,int> G[200002];
int visited[200002];
long long int cc_size;
long long int loop_size;
int loop_start;

int dfs(int u, int p) {

    visited[u] = 1;
    cc_size++;
    int res = 0;
    for(auto it = G[u].begin(); it != G[u].end(); it++) {
        if( !visited[it->first] ) {
            if(dfs(it->first, u) ) {
                loop_size++;
                if( u != loop_start ) {
                    res = 1;
                }
            }
        } else {
            if( it->second == 2 || it->first != p ) { // loop found
                loop_start = it->first;
                loop_size++;
                res = 1;
            }

        }
    }

    return res;
}

long long int powMod(long long int base, long long int p, long long int MOD) {
    long long int res = 1;
    while(p ) {
        if( p & 1 ) {
            res = (res * base)%MOD;
        }

        base = (base*base)%MOD;
        p >>= 1;
    }
    return res;
}

int main() {
    cin >> n;
    for(int u = 1; u <= n; u++) {
        cin >> v;

        G[u][v]++;
        G[v][u]++;
    }

    long long int res = powMod(2, n, M);
    for(int u = 1; u <= n; u++) {
        if( !visited[u] ) {
            cc_size = 0;
            loop_size = 0;
            loop_start = -1;
            dfs(u, -1);
            res = (res - powMod(2, cc_size - loop_size + 1, M) + M)%M;
        }
    }

    cout << res << endl;

    return 0;
}