#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
const long long int inf = 2e9 + 5;
double PI = 3.14159265358979323846;
long long int mpow(long long int a, long long int b, long long int m = mod) {
  long long int ans = 1;
  while (b) {
    if (b & 1ll) {
      ans *= a;
      ans %= m;
    }
    a *= a;
    a %= m;
    b = b >> 1;
  }
  return ans;
}
const int N = 2e5 + 5;
vector<bool> visited(N, false);
vector<bool> visited2(N, false);
int n;
vector<int> a(N);
int cycleLength;
vector<int> depth(N, 0);
void dfs(int u, int dep = 0) {
  if (visited2[u]) {
    return;
  }
  if (visited[u]) {
    cycleLength = dep - depth[u];
    return;
  }
  depth[u] = dep;
  visited[u] = true;
  dfs(a[u], dep + 1);
}
void solve() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  long long int ans = 1;
  for (int i = 1; i <= n; i++) {
    if (!visited[i]) {
      cycleLength = 0;
      dfs(i);
      int chain = 0;
      int u = i;
      while (!visited2[u]) {
        visited2[u] = true;
        chain++;
        u = a[u];
      }
      if (cycleLength > 0) {
        ans *=
            (mpow(2, chain - cycleLength) * (mpow(2, cycleLength) - 2)) % mod;
        ans %= mod;
      } else {
        ans *= mpow(2, chain);
        ans %= mod;
      }
    }
  }
  cout << ans << "\n";
}
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}
