#include <bits/stdc++.h>
using namespace std;
long long par[200010], vis[200010];
bool prime[10000001];
void SieveOfErat() {
  memset(prime, true, sizeof(prime));
  for (long long p = 2; p * p <= 1000000; p++) {
    if (prime[p] == true) {
      for (long long i = p * 2; i <= 1000000; i += p) prime[i] = false;
    }
  }
}
int gcd(int x, int y) {
  if (x == 0) {
    return y;
  } else
    return gcd(x % y, y);
}
int main() {
  long long n, c, j;
  long long curr = 0;
  scanf("%lld", &n);
  long long pw[200010] = {0};
  pw[0] = 1;
  for (long long i = 1; i <= n; i++) {
    pw[i] = (pw[i - 1] << 1) % 1000000007;
  }
  for (long long i = 1; i <= n; i++) {
    scanf("%lld", &par[i]);
  }
  long long res = 1;
  long long k = n;
  for (long long i = 1; i <= n; i++) {
    if (!vis[i]) {
      long long final = curr;
      j = i;
      while (!vis[j]) {
        vis[j] = ++curr;
        j = par[j];
      }
      if (vis[j] > final) {
        c = curr - vis[j] + 1;
        res = 1ll * res * (pw[c] - 2 + 1000000007) % 1000000007;
        k -= c;
      }
    }
  }
  res = 1ll * res * pw[k] % 1000000007;
  printf("%lld", res);
}
