#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5, M = 2e4 + 5, inf = 0x3f3f3f3f, mod = 1e9 + 7;
void Print(int *a, int n) {
  for (int i = 1; i < n; i++) printf("%d ", a[i]);
  printf("%d\n", a[n]);
}
int n, h[N], cnt, vis[N];
int b[N], id, dep[N];
struct edge {
  int to, nt;
} e[N << 1];
void add(int u, int v) { e[++cnt] = {v, h[u]}, h[u] = cnt; }
void dfs(int u, int d) {
  vis[u] = 1, dep[u] = d;
  for (int i = h[u]; i; i = e[i].nt) {
    int v = e[i].to;
    if (!vis[v])
      dfs(v, d + 1);
    else if (vis[v] == 1)
      b[++id] = dep[u] - dep[v] + 1;
  }
  vis[u] = 2;
}
long long ksm(long long a, long long n, int m = mod) {
  long long ans = 1;
  while (n) {
    if (n & 1) ans = ans * a % m;
    a = a * a % m;
    n >>= 1;
  }
  return ans;
}
int main() {
  scanf("%d", &n);
  long long ans = 1, sum = 0;
  for (int i = 1, j; i <= n; i++) {
    scanf("%d", &j);
    add(i, j);
  }
  for (int i = 1; i <= n; i++)
    if (!dep[i]) dfs(i, 1);
  for (int i = 1; i <= id; i++)
    sum += b[i], ans = ans * (ksm(2, b[i]) - 2) % mod;
  ans = ans * ksm(2, n - sum) % mod;
  printf("%lld\n", ans);
  return 0;
}
