#include <bits/stdc++.h>
using namespace std;
long long a[2 * 100000 + 7];
long long p[2 * 100000 + 7];
long long cm[2 * 100000 + 7];
long long parent(long long u) {
  if (u == p[u]) return u;
  return p[u] = parent(p[u]);
}
long long floyd(long long x0) {
  long long tor, har, mu, lam;
  tor = a[x0];
  har = a[a[x0]];
  while (tor != har) {
    tor = a[tor];
    har = a[a[har]];
  }
  mu = 0;
  tor = x0;
  while (tor != har) {
    tor = a[tor];
    har = a[har];
    mu++;
  }
  lam = 1;
  har = a[tor];
  while (tor != har) {
    har = a[har];
    lam++;
  }
  return lam;
}
long long power(long long a, long long n) {
  if (!n) return 1;
  long long r = power(a, n / 2);
  r = (r * r) % 1000000007;
  if (n % 2) r = (r * a) % 1000000007;
  return r;
}
int main(void) {
  std::ios::sync_with_stdio(false);
  long long n, c, i, j, u, v, cn;
  cin >> n;
  for (i = 1; i <= n; i++) cin >> a[i], p[i] = i;
  for (i = 1; i <= n; i++) {
    u = i;
    v = a[i];
    u = parent(u);
    v = parent(v);
    if (u > v) swap(u, v);
    p[v] = u;
  }
  c = 0;
  for (i = 1; i <= n; i++) {
    parent(i);
    cm[p[i]]++;
  }
  cn = 1;
  for (i = 1; i <= n; i++) {
    if (p[i] == i) {
      c = floyd(i);
      j = power(2, cm[i]) - power(2, cm[i] - c + 1);
      j = (j % 1000000007 + 1000000007) % 1000000007;
      cn = (cn * j) % 1000000007;
    }
  }
  cout << cn << endl;
  return 0;
}
