#include <bits/stdc++.h>
using namespace std;
int n, a[200010], fir[200010], nxt[200010 << 1], to[200010 << 1],
    tot(1), vis[200010], times(0), e(0),
    tl(0), len[200010], fre[200010], fr[200010], num[200010];
long long p[200010];
inline int read() {
  int s(0);
  char c = getchar();
  while (!isdigit(c)) c = getchar();
  while (isdigit(c)) s = s * 10 + c - '0', c = getchar();
  return s;
}
void add(int u, int v) {
  to[++tot] = v;
  nxt[tot] = fir[u];
  fir[u] = tot;
}
void dfs(int x, int f, int t) {
  vis[x] = 1;
  num[x] = ++times;
  for (int i = fir[x]; i && e == 0; i = nxt[i]) {
    if (vis[to[i]]) {
      if (num[to[i]] < t) return;
      e = x;
      len[++tot] = num[x] - num[to[i]] + 1;
      tl += len[tot];
      return;
    }
    fr[to[i]] = x;
    fre[to[i]] = i;
    dfs(to[i], x, t);
  }
}
long long mpow(long long a, long long b) {
  long long s(1);
  while (b) {
    if (b & 1) s = s * a % 1000000007;
    a = a * a % 1000000007;
    b >>= 1;
  }
  return s;
}
int main() {
  n = read();
  for (int i = 1; i <= n; i++) a[i] = read(), add(i, a[i]);
  tot = 0;
  for (int i = 1; i <= n; i++)
    if (!vis[i]) e = 0, dfs(i, 0, times + 1);
  p[0] = 1;
  for (int i = 1; i <= n; i++) p[i] = p[i - 1] * 2 % 1000000007;
  long long ans = p[n - tl];
  for (int i = 1; i <= tot; i++) ans = ans * (p[len[i]] - 2) % 1000000007;
  printf("%I64d\n", ans);
  return 0;
}
