#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
vector<int> v[200010];
int vis[200010];
int tot;
int huan;
int deep[200010];
long long fastmod(long long a, int b) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans = (ans * a) % mod;
    a = a * a % mod;
    b /= 2;
  }
  return ans;
}
void dfs(int u, int d, int zu) {
  int sz = v[u].size();
  vis[u] = zu;
  tot++;
  deep[u] = d;
  for (int i = 0; i <= sz - 1; i++) {
    int vv = v[u][i];
    if (vis[vv] == zu) {
      huan = d - deep[vv] + 1;
    } else {
      if (vis[vv] == 0) dfs(vv, d + 1, zu);
    }
  }
}
int main() {
  memset(vis, 0, sizeof(vis));
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    v[i].push_back(x);
  }
  long long ans = 1;
  int cnt = 0;
  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      tot = 0;
      huan = 0;
      cnt++;
      dfs(i, 1, cnt);
      if (huan == 0)
        ans = (ans * fastmod(2, tot)) % mod;
      else {
        ans = ((ans * ((fastmod(2, huan) - 2 + mod) % mod)) % mod *
               fastmod(2, tot - huan)) %
              mod;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
