#include <bits/stdc++.h>
using namespace std;
const int kN = 2 * 100 * 1000;
const int kMod = 1000 * 1000 * 1000 + 7;
int a[kN];
int rk[kN];
int used[kN];
vector<long long> cnt;
long long Pow(long long n, long long k) {
  long long res = 1;
  long long carry = n;
  while (k) {
    if (k & 1) {
      res = res * carry % kMod;
    }
    carry = carry * carry % kMod;
    k >>= 1;
  }
  return res;
}
void DFS(int u, int r) {
  if (used[u] == 2) {
    return;
  }
  if (used[u] == 1) {
    cnt.push_back(r - rk[u]);
    used[u] = 2;
    return;
  }
  rk[u] = r;
  used[u] = 1;
  DFS(a[u], r + 1);
  used[u] = 2;
}
int main() {
  std::ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    --a[i];
  }
  for (int i = 0; i < n; ++i) {
    DFS(i, 0);
  }
  long long res = 1;
  long long sum = 0;
  for (long long c : cnt) {
    sum += c;
    res = res * (Pow(2, c) + kMod - 2) % kMod;
  }
  res = res * Pow(2, n - sum) % kMod;
  cout << res << '\n';
  return 0;
}
