#include <bits/stdc++.h>
namespace wxy {
const int N = 2e5 + 5, mod = 1e9 + 7;
long long head[N], fail[N << 1], edge[N << 1], tot;
long long n;
long long fa[N], fat[N], dep[N], size[N], pu[N], pv[N], au[N], av[N];
bool vis[N];
inline void add(long long x, long long y) {
  edge[++tot] = y;
  fail[tot] = head[x];
  head[x] = tot;
}
inline long long get(long long x) {
  if (fa[x] == x)
    return x;
  else
    return fa[x] = get(fa[x]);
}
inline void merge(long long x, long long y) { fa[get(x)] = fa[get(y)]; }
inline long long ksm(long long b) {
  long long a = 2;
  long long res = 1;
  for (; b; b >>= 1) {
    if (b & 1) res = 1ll * res * a % mod;
    a = 1ll * a * a % mod;
  }
  return res;
}
inline void dfs(long long x) {
  vis[x] = true;
  size[x] = 1;
  for (long long i = head[x]; i; i = fail[i]) {
    long long v = edge[i];
    if (vis[v]) continue;
    fat[v] = x;
    dep[v] = dep[x] + 1;
    dfs(v);
    size[x] += size[v];
  }
}
inline long long dis(long long x, long long y) {
  if (dep[x] < dep[y]) std::swap(x, y);
  long long delta = dep[x] - dep[y];
  long long res = delta;
  for (long long i = 0; i < delta; i++) x = fat[x];
  if (x == y) return res;
  while (x != y) {
    x = fat[x];
    y = fat[y];
    res += 2;
  }
  return res;
}
void main() {
  tot = 0;
  std::cin >> n;
  for (long long i = 1; i <= n; i++) fa[i] = i;
  long long cnt = 0;
  for (long long i = 1; i <= n; i++) {
    long long to;
    std::cin >> to;
    if (get(i) != get(to)) {
      merge(i, to);
      add(i, to);
      add(to, i);
    } else {
      pu[++cnt] = i;
      pv[cnt] = to;
    }
  }
  for (long long i = 1; i <= cnt; i++) {
    long long u = pu[i], v = pv[i];
    au[get(u)] = u;
    av[get(u)] = v;
  }
  long long ans = 1;
  for (long long i = 1; i <= n; i++) {
    if (fa[i] == i) {
      dfs(i);
      long long d = dis(au[i], av[i]);
      long long res1 = (ksm(d + 1) - 2 + mod) % mod;
      long long res2 = 1ll * res1 * ksm(size[i] - d - 1) % mod;
      ans = 1ll * ans * res2 % mod;
    }
  }
  std::cout << ans;
}
}  // namespace wxy
signed main() {
  wxy::main();
  return 0;
}
