#include <bits/stdc++.h>
typedef int nlist[200010];
nlist be, next, v, vr, nextr, ber, vst, dfn, p2;
int nl, num = 0;
void addedge(int a, int b, int id) {
  next[id] = be[a];
  v[id] = b;
  be[a] = id;
}
void addedger(int a, int b, int id) {
  nextr[id] = ber[a];
  vr[id] = b;
  ber[a] = id;
}
void dfs1(int now) {
  vst[now] = 1;
  for (int j = be[now]; j; j = next[j]) {
    if (!vst[v[j]]) {
      dfs1(v[j]);
    }
  }
  dfn[++num] = now;
}
void dfs2(int now) {
  vst[now] = 2;
  for (int j = ber[now]; j; j = nextr[j]) {
    if (vst[vr[j]] != 2) {
      dfs2(vr[j]);
    }
  }
  nl++;
}
int main(void) {
  int n;
  scanf("%d", &n);
  p2[0] = 1;
  for (int i = 1; i <= n; i++) be[i] = ber[i] = 0;
  for (int i = 1, a; i <= n; i++) {
    scanf("%d", &a);
    addedge(i, a, i);
    addedger(a, i, i);
    vst[i] = 0;
    p2[i] = p2[i - 1] * 2 % 1000000007;
  }
  for (int i = 1; i <= n; i++)
    if (!vst[i]) dfs1(i);
  int ans = 1, tot = n;
  for (int j = n, i; j; j--)
    if (vst[i = dfn[j]] != 2) {
      nl = 0;
      dfs2(i);
      if (nl == 1) continue;
      ans = (long long)ans * (p2[nl] - 2 + 1000000007) % 1000000007;
      tot -= nl;
    }
  while (tot--) ans = (ans * 2) % 1000000007;
  printf("%d", ans);
}
