#include <bits/stdc++.h>
using namespace std;
const int nmax = 2e5 + 5;
const int mod = 1e9 + 7;
long long twos[nmax];
long long ans = 1;
long long cnt;
int a[nmax];
int vis[nmax];
vector<int> cycles;
void precal(int n) {
  twos[0] = 1;
  for (int i = 1; i <= n; i++) twos[i] = (twos[i - 1] * 2LL) % mod;
}
void dfs2(int u) {
  vis[u] = 3;
  cycles[cnt]++;
  if (vis[a[u]] == 3) return;
  dfs2(a[u]);
}
void dfs(int u) {
  vis[u] = 2;
  if (vis[a[u]] == 0)
    dfs(a[u]);
  else if (vis[a[u]] == 1) {
    vis[u] = 1;
    return;
  } else {
    cycles.push_back(0);
    dfs2(u);
    cnt++;
  }
  vis[u] = 1;
  return;
}
int main() {
  int n;
  scanf("%d", &n);
  precal(n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= n; i++) {
    if (vis[i] == 0) {
      dfs(i);
    }
  }
  long long tot = n;
  for (int i = 0; i < cycles.size(); i++) {
    tot -= cycles[i];
    ans = (ans * (twos[cycles[i]] - 2 + mod)) % mod;
  }
  ans = (ans * (twos[tot])) % mod;
  if (ans < 0) ans += mod;
  cout << ans;
  return 0;
}
