#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int t = 1, sum = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') t = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') sum = sum * 10 + ch - '0', ch = getchar();
  return t * sum;
}
const int Max = 2e5 + 10, mod = 1e9 + 7;
int a[Max], d[Max], siz[Max], f[Max], bin[Max], vis[Max], q[Max], tot, n,
    ans = 1;
int main() {
  n = read();
  bin[0] = 1;
  for (int i = 1; i <= n; i++) bin[i] = 1LL * bin[i - 1] * 2 % mod;
  for (int i = 1; i <= n; i++) a[i] = read(), d[a[i]]++;
  for (int i = 1; i <= n; i++)
    if (!d[i]) q[++tot] = i, f[q[tot]] = 1;
  for (int i = 1, x; i <= tot; i++) {
    x = q[i];
    if (!f[a[x]]) f[a[x]] = 1;
    f[a[x]] = 1LL * f[a[x]] * f[x] * 2 % mod;
    if (--d[a[x]] == 0) q[++tot] = a[x];
  }
  for (int i = 1, v, res; i <= n; i++)
    if (d[i] && !vis[i]) {
      res = 1;
      v = i;
      do {
        siz[i]++;
        res = 1LL * res * (f[v] == 0 ? 1 : f[v]) % mod;
        vis[v] = 1;
        v = a[v];
      } while (v != i);
      ans = 1LL * ans * (bin[siz[i]] - 2 + mod) % mod * res % mod;
    }
  printf("%d\n", ans);
  return 0;
}
