#include <bits/stdc++.h>
using namespace std;
template <typename T>
struct SegmentTree {};
long long n;
long long numSCC;
vector<vector<long long> > g;
long long low[200005], num[200005], cur;
bitset<200005> visited;
stack<long long> st;
long long cnt[200005];
void input() {
  scanf("%lld", &n);
  g.assign(n, vector<long long>());
  for (long long i = 0; i < n; ++i) {
    long long x;
    scanf("%lld", &x);
    g[i].push_back(--x);
  }
  visited.reset();
  memset(num, -1, sizeof num);
  cur = 0;
  numSCC = 0;
}
void tarjanSCC(long long v) {
  low[v] = num[v] = cur++;
  st.push(v);
  visited[v] = true;
  for (long long u : g[v]) {
    if (num[u] == -1) tarjanSCC(u);
    if (visited[u]) low[v] = min(low[u], low[v]);
  }
  if (low[v] == num[v]) {
    ++numSCC;
    while (true) {
      long long u = st.top();
      st.pop();
      visited[u] = 0;
      cnt[numSCC]++;
      if (u == v) break;
    }
  }
}
void solve() {
  for (long long i = 0; i < n; ++i)
    if (num[i] == -1) tarjanSCC(i);
  long long edgeSCC = 0;
  long long ans = 1;
  for (long long i = 1; i <= numSCC; ++i) {
    edgeSCC += (cnt[i] > 1 ? cnt[i] : 0);
    long long scc = 1;
    if (cnt[i] > 1) {
      for (long long j = 1; j <= cnt[i]; ++j) scc *= 2, scc %= 1000000007;
      scc += 1000000007 - 2;
      scc %= 1000000007;
    }
    ans *= scc;
    ans %= 1000000007;
  }
  for (long long i = 1; i <= n - edgeSCC; ++i) ans *= 2, ans %= 1000000007;
  printf("%lld", ans);
}
signed main(signed argc, char** argv) {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  input();
  solve();
  return 0;
}
