#include <bits/stdc++.h>
using namespace std;
int vis[200001], power[200001], mod = 1e9 + 7, edge[200001], dis[200001];
int ans = 1;
int tot = 0;
void dfs(int x, int d) {
  dis[x] = d;
  int y = edge[x];
  vis[x] = 1;
  if (vis[y] == 1) {
    int len = dis[x] - dis[y] + 1;
    tot -= len;
    ans = ans * 1ll * (power[len] - 2 + mod) % mod;
  } else if (!vis[y]) {
    dfs(y, d + 1);
  }
  vis[x] = 2;
}
int main() {
  power[0] = 1;
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &edge[i]);
  }
  for (int i = 1; i <= n; i++) power[i] = power[i - 1] * 2 % mod;
  tot = n;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      dfs(i, 0);
    }
  }
  ans = ans * 1ll * power[tot] % mod;
  cout << ans;
  return 0;
}
