#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > g(2 * 100005);
int visited[2 * 100005] = {0}, cyc[2 * 100005], j = 0, par[2 * 100005];
void dfs(int v, int parent) {
  visited[v] = 1;
  par[v] = parent;
  for (auto i : g[v]) {
    if (!visited[i])
      dfs(i, v);
    else {
      int gr = 2, x = v;
      while (par[x] != 0) {
        if (par[x] == i) {
          cyc[j++] = gr;
          break;
        }
        x = par[x];
        gr++;
      }
    }
  }
}
int main() {
  int n, a, sum = 0;
  long long int pro, ans = 1;
  cin >> n;
  for (int i = 0; i < int(n); ++i) cin >> a, g[i + 1].push_back(a);
  for (int i = 0; i < int(n); ++i)
    if (visited[i + 1] != 1) dfs(i + 1, 0);
  for (int i = 0; i < int(j); ++i) {
    pro = 1LL;
    for (int s = 0; s < int(cyc[i]); ++s) pro = (pro * 2LL) % 1000000007;
    sum += cyc[i];
    pro = 1LL * (1000000007 + pro - 2) % 1000000007;
    ans = (ans * pro) % 1000000007;
  }
  for (int i = 0; i < int(n - sum); ++i) ans = (ans * 2LL) % 1000000007;
  cout << ans;
  return 0;
}
