#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200100;
const long long MOD = 1000000007;
int n, a[MAXN], tot = 0, len[MAXN];
long long f[MAXN];
int main() {
  scanf("%d", &n);
  int x;
  f[0] = 1;
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    f[i] = f[i - 1] * 2 % MOD;
  }
  tot = 0;
  long long ans = 1;
  int rest = n;
  for (int i = 1; i <= n; i++) {
    if (!len[i]) {
      int pre = tot;
      int now = i;
      while (!len[now]) {
        tot++;
        len[now] = tot;
        now = a[now];
      }
      if (len[now] > pre) {
        ans = ans * (f[tot + 1 - len[now]] - 2) % MOD;
        if (ans < 0) ans += MOD;
        rest -= tot + 1 - len[now];
      }
    }
  }
  ans = ans * f[rest] % MOD;
  printf("%I64d\n", ans);
  return 0;
}
