#include <bits/stdc++.h>
namespace IO {
char ibuf[(1 << 20)], *iS = ibuf + (1 << 20), *iT = ibuf + (1 << 20);
template <class T>
inline void read(T &x) {
  x = 0;
  int f = 0;
  char ch = ((iS == iT) ? (iT = (iS = ibuf) + fread(ibuf, 1, (1 << 20), stdin),
                           iS == iT ? EOF : *iS++)
                        : *iS++);
  for (; !isdigit(ch);
       f |= ch == '-',
       ch = ((iS == iT) ? (iT = (iS = ibuf) + fread(ibuf, 1, (1 << 20), stdin),
                          iS == iT ? EOF : *iS++)
                        : *iS++))
    ;
  for (; isdigit(ch);
       x = (x << 1) + (x << 3) + (ch ^ 48),
       ch = ((iS == iT) ? (iT = (iS = ibuf) + fread(ibuf, 1, (1 << 20), stdin),
                          iS == iT ? EOF : *iS++)
                        : *iS++))
    ;
  x = (f == 1 ? ~x + 1 : x);
}
char Out[(1 << 20)], *iter = Out;
template <class T>
inline void write(T x, char ch = '\n') {
  T l, c[35];
  if (x < 0) *iter++ = '-', x = ~x + 1;
  for (l = 0; !l || x; c[l] = x % 10, l++, x /= 10)
    ;
  for (; l; --l, *iter++ = c[l] + '0')
    ;
  *iter++ = ch;
  fwrite(Out, 1, iter - Out, stdout), iter = Out;
}
}  // namespace IO
using namespace IO;
using namespace std;
struct Edge {
  long long x, y;
} e[200005 * 2];
long long n, m, sz[200005], tot, a[200005], vis[200005], MOD = 1e9 + 7;
long long cnt, head[200005], nxt[200005 * 2], dep[200005];
void Add(long long x, long long y) {
  e[++cnt] = (Edge){x, y};
  nxt[cnt] = head[x];
  head[x] = cnt;
}
void Dfs(long long x, long long d) {
  dep[x] = d, vis[x] = 1;
  for (long long i = head[x]; i; i = nxt[i]) {
    long long y = e[i].y;
    if (!vis[y])
      Dfs(y, d + 1);
    else if (vis[y] == 1)
      sz[++tot] = dep[x] - dep[y] + 1, m += dep[x] - dep[y] + 1;
  }
  vis[x] = 2;
}
long long Qpow(long long a, long long b) {
  long long res = 1;
  while (b) {
    if (b & 1) res = res * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return res;
}
signed main() {
  read(n);
  for (long long i = 1; i <= n; i++) {
    read(a[i]), Add(i, a[i]);
  }
  for (long long i = 1; i <= n; i++)
    if (!dep[i]) Dfs(i, 1);
  long long ans;
  ans = Qpow(2, n - m);
  for (long long i = 1; i <= tot; i++) {
    ans = 1ll * ans * (Qpow(2, sz[i]) - 2) % MOD;
  }
  write(ans);
  return 0;
}
