#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
int n, t, m, q, k, p;
int col[1 << 18], G[1 << 18];
long long int ans = 1ll, temp = 1ll;
int dfs(int v) {
  col[v] = 1;
  int to = G[v], cyclev = -1;
  if (col[to] == 1) {
    cyclev = to;
    temp = 2ll;
  } else if (col[to] == 2) {
    ans = ans * 2ll % mod;
  } else {
    cyclev = dfs(to);
    if (cyclev == v) {
      temp = (temp * 2ll - 2ll) % mod;
      cyclev = -1;
      ans = ans * temp % mod;
    } else if (cyclev != -1)
      temp = temp * 2ll % mod;
    else
      ans = ans * 2ll % mod;
  }
  col[v] = 2;
  return cyclev;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  for (int i = 0; i < n; ++i) cin >> G[i + 1];
  for (int i = 1; i <= n; ++i)
    if (!col[i]) dfs(i);
  cout << ans << '\n';
  cin >> n;
}
