#include <bits/stdc++.h>
using namespace std;
vector<int> G[200000 + 5];
int n, f[200000 + 5], dep[200000 + 5], vis[200000 + 5];
int siz[200000 + 5], pow2[200000 + 5], tot, sum;
long long ans;
void pt(int x) {
  vis[x] = 1;
  for (int i = 0; i < G[x].size(); i++)
    if (!vis[G[x][i]]) pt(G[x][i]);
}
void dfs(int x, int p) {
  if (dep[x]) {
    siz[tot] = p - dep[x];
    return;
  }
  dep[x] = p;
  dfs(f[x], p + 1);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &f[i]);
    G[f[i]].push_back(i);
    G[i].push_back(f[i]);
  }
  for (int i = 1; i <= n; i++)
    if (!vis[i]) {
      pt(i);
      tot++;
      dfs(i, 1);
      sum += siz[tot];
    }
  pow2[0] = 1;
  for (int i = 1; i <= n; i++) pow2[i] = (pow2[i - 1] * 2LL) % 1000000007;
  ans = pow2[n - sum];
  for (int i = 1; i <= tot; i++)
    ans = (1LL * ans * (pow2[siz[i]] - 2)) % 1000000007;
  printf("%lld", ans);
}
