#include <bits/stdc++.h>
using namespace std;
int n, a[200011], c[200011], isc[200011], cn, pow2[200011] = {1}, seen[200011];
int main() {
  int ans = 1, mod = 1e9 + 7;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", a + i);
    pow2[i] = pow2[i - 1] * 2LL % mod;
  }
  for (int i = 1; i <= n; i++) {
    if (seen[i] < 0) continue;
    ++cn;
    int j;
    for (j = i; seen[j] >= 0; j = a[j]) seen[j] = -cn, ++c[cn];
    if (seen[j] == -cn) isc[cn] = 1;
    if (isc[cn]) {
      int clen = 1;
      for (int k = a[j]; seen[k] == -cn && k != j; k = a[k]) ++clen;
      ans = (ans * 1LL * (pow2[clen] - 2 + mod) % mod) % mod;
      ans = (ans * 1LL * pow2[c[cn] - clen]) % mod;
    } else {
      ans = (ans * 1LL * pow2[c[cn]]) % mod;
    }
  }
  printf("%d\n", ans);
  return 0;
}
