#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
const int MOD = 1e9 + 7;
int n, a[maxn];
int lown[maxn], dfn[maxn], sccno[maxn], dfs_clk, scc_cnt;
stack<int> stk;
vector<int> g[maxn];
bool vis[maxn];
void init() {
  memset(lown, 0, sizeof lown);
  memset(dfn, 0, sizeof dfn);
  memset(sccno, 0, sizeof sccno);
}
long long quick_mod(int x, int n) {
  long long ret = 1, cur = x;
  while (n > 0) {
    if (n & 1) {
      ret = (ret * cur) % MOD;
    }
    cur = (cur * cur) % MOD;
    n >>= 1;
  }
  return ret;
}
void Tarjan(int u) {
  lown[u] = dfn[u] = ++dfs_clk;
  stk.push(u);
  int v = a[u];
  if (!dfn[v]) {
    Tarjan(v);
    lown[u] = min(lown[u], lown[v]);
  } else if (!sccno[v]) {
    lown[u] = min(lown[u], dfn[v]);
  }
  if (lown[u] == dfn[u]) {
    scc_cnt++;
    while (true) {
      int x = stk.top();
      stk.pop();
      g[scc_cnt].push_back(x);
      sccno[x] = scc_cnt;
      if (x == u) break;
    }
  }
}
bool isCycle(vector<int> x) {
  memset(vis, false, sizeof vis);
  for (int i = 0; i < x.size(); i++) {
    int v = a[x[i]];
    int u = x[i];
    if (!vis[v] || !vis[u]) {
      vis[v] = vis[u] = true;
    } else
      return true;
  }
  return false;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n;
  init();
  long long ans = 1;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  for (int i = 1; i <= n; i++) {
    if (!dfn[i]) {
      Tarjan(i);
    }
  }
  for (int i = 1; i <= scc_cnt; i++) {
    int sz = g[i].size();
    if (isCycle(g[i])) {
      ans = (ans * ((quick_mod(2, sz) - 2 + MOD) % MOD)) % MOD;
    } else {
      ans = (ans * quick_mod(2, sz)) % MOD;
    }
  }
  cout << ans << endl;
  return 0;
}
