#include <bits/stdc++.h>
using namespace std;
long long MOD = (long long)1e9 + 7;
template <typename T>
T modpow(T base, T exp, T modulus) {
  base %= modulus;
  T result = 1;
  while (exp > 0) {
    if (exp & 1) result = (result * base) % modulus;
    base = (base * base) % modulus;
    exp >>= 1;
  }
  return result;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    v[i] = a - 1;
  }
  long long ans = 1;
  vector<bool> seen(n);
  vector<bool> cyclepart(n);
  for (int i = 0; i < n; i++) {
    if (seen[i]) continue;
    vector<int> trail;
    set<int> prevs;
    int cur = i;
    while (true) {
      if (prevs.count(cur)) {
        int idx = 0;
        while (trail[idx] != cur) idx++;
        ans *= modpow((long long)2, (long long)trail.size() - idx, MOD) - 2;
        ans %= MOD;
        for (int asd = idx; asd < trail.size(); asd++) {
          cyclepart[trail[asd]] = true;
        }
        break;
      }
      prevs.insert(cur);
      trail.emplace_back(cur);
      cur = v[cur];
      if (seen[cur] && !prevs.count(cur)) break;
      seen[cur] = true;
    }
  }
  long long qwe = 0;
  for (auto a : cyclepart) {
    if (!a) qwe++;
  }
  if (qwe != 0) ans *= modpow((long long)2, qwe, MOD);
  ans %= MOD;
  cout << ans;
}
