#include <bits/stdc++.h>
using namespace std;
const int N = 200100, mod = 1e9 + 7;
int n, s, tot = 1, ans, head[N], ver[N << 1], nex[N << 1], cnt, w[N], use,
          dfn[N], to;
inline void add(int x, int y) {
  ver[++tot] = y;
  nex[tot] = head[x];
  head[x] = tot;
}
inline int q_mul(int x, int y) {
  int to = 1;
  while (y) {
    if (y & 1) to = 1ll * to * x % mod;
    x = 1ll * x * x % mod;
    y >>= 1;
  }
  return to;
}
inline void dfs(int x, int f, int from) {
  dfn[x] = dfn[f] + 1;
  for (register int i = head[x]; i; i = nex[i]) {
    if (i == (from ^ 1)) continue;
    if (s != x && dfn[ver[i]]) {
      w[cnt] = abs(dfn[x] - dfn[ver[i]]) + 1;
      continue;
    }
    if (!dfn[ver[i]]) dfs(ver[i], x, i);
  }
}
int main() {
  scanf("%d", &n);
  for (register int x, i = 1; i <= n; i++) {
    scanf("%d", &x);
    add(i, x);
    add(x, i);
  }
  for (register int i = 1; i <= n; i++) {
    if (!dfn[i]) {
      s = i;
      dfn[i] = 1;
      cnt++;
      dfs(i, 0, 0);
      use += w[cnt];
    }
  }
  ans = q_mul(2, n - use);
  for (register int i = 1; i <= cnt; i++) {
    ans = 1ll * ans * ((q_mul(2, w[i]) - 2 + mod) % mod) % mod;
  }
  printf("%d\n", ans);
  return 0;
}
