#include <bits/stdc++.h>
int Pow(int n, int m) {
  int res = 1;
  while (m) {
    if (m & 1) res = 1LL * res * n % (int)(1e9 + 7);
    m >>= 1;
    n = 1LL * n * n % (int)(1e9 + 7);
  }
  return res;
}
int son[200005], degree[200005], Q[200005 << 1], L = 0, R = 0;
bool vis[200005];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &son[i]);
    degree[son[i]]++;
  }
  for (int i = 1; i <= n; i++)
    if (!degree[i]) {
      Q[R++] = i;
      vis[i] = 1;
    }
  while (L < R) {
    int u = Q[L++];
    vis[u] = 1;
    degree[son[u]]--;
    if (!degree[son[u]]) Q[R++] = son[u];
  }
  int tmp = n;
  long long ans = 1;
  for (int i = 1; i <= n; i++)
    if (!vis[i]) {
      int st = son[i], cnt = 1;
      vis[i] = 1;
      while (st != i) {
        vis[st] = 1;
        cnt++;
        st = son[st];
      }
      ans = ans * (Pow(2, cnt) - 2 + (int)(1e9 + 7)) % (int)(1e9 + 7);
      tmp -= cnt;
    }
  ans = ans * Pow(2, tmp) % (int)(1e9 + 7);
  printf("%d\n", (int)ans);
  return 0;
}
