#include <bits/stdc++.h>
using namespace std;
const int N = 200005;
const int mod = 1e9 + 7;
int n, a[N], p[N], prv[N];
int main() {
  scanf("%d", &n);
  int total = n;
  for (int i = 0; i < n; ++i) {
    scanf("%d", a + i);
    --a[i];
  }
  int result = 1;
  for (int i = 0; i < n; ++i) {
    if (!p[i]) {
      int x = i;
      do {
        p[x] = 1;
        prv[a[x]] = x;
        x = a[x];
      } while (!p[x]);
      int y = prv[x], cnt = 1;
      while (y != i && y != x) y = prv[y], ++cnt;
      if (y == x) {
        int val = 1;
        for (int i = 0; i < cnt; ++i) {
          val *= 2;
          val %= mod;
        }
        val = (val - 2 + mod) % mod;
        result = (1LL * result * val) % mod;
        total -= cnt;
      }
    }
  }
  for (int i = 0; i < total; ++i) {
    result *= 2;
    result %= mod;
  }
  printf("%d\n", result);
  return 0;
}
