#include <bits/stdc++.h>
using namespace std;
const int maxn = (int)2e5 + 100;
const long long mod = (long long)1e9 + 7;
int GNIL[maxn];
int used[maxn];
long long d[maxn];
vector<int> g[maxn];
int dfs(int v) {
  if (used[v]) return v;
  used[v] = 1;
  return dfs(GNIL[v]);
}
void dfs2(int v) {
  used[v] = 2;
  for (int i = 0; i < (int)g[v].size(); ++i)
    if (used[g[v][i]] != 2) dfs2(g[v][i]);
}
int main(int argc, const char* argv[]) {
  d[0] = 1;
  for (int i = 1; i < maxn; ++i) d[i] = (d[i - 1] * 2ll) % mod;
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i)
    scanf("%d", &GNIL[i]), --GNIL[i], g[i].push_back(GNIL[i]),
        g[GNIL[i]].push_back(i);
  vector<int> L;
  for (int i = 0; i < n; ++i) {
    if (!used[i]) {
      int v0 = dfs(i), v = GNIL[v0];
      L.push_back(1);
      while (v != v0) ++L[L.size() - 1], v = GNIL[v];
      dfs2(v0);
    }
  }
  int rest = n;
  for (int i = 0; i < (int)L.size(); ++i) rest -= L[i];
  long long ans = d[rest];
  for (int i = 0; i < (int)L.size(); ++i) ans *= (d[L[i]] - 2ll), ans %= mod;
  cout << ans;
  return 0;
}
