#include <bits/stdc++.h>
long long mpow(long long a, long long n, long long mod) {
  long long ret = 1;
  long long b = a;
  while (n) {
    if (n & 1) ret = (ret * b) % mod;
    b = (b * b) % mod;
    n >>= 1;
  }
  return (long long)ret;
}
using namespace std;
vector<int> G[(int)(2e5 + 25)];
int a[(int)(2e5 + 25)], dfs_timer[(int)(2e5 + 25)], dfs_min[(int)(2e5 + 25)],
    timer, c = 1;
int sz[(int)(2e5 + 25)], inScc[(int)(2e5 + 25)];
bool vis[(int)(2e5 + 25)];
stack<int> s;
void dfs(int x) {
  dfs_timer[x] = ++timer;
  dfs_min[x] = dfs_timer[x];
  s.push(x);
  vis[x] = 1;
  for (int i = 0; i < G[x].size(); i++) {
    int to = G[x][i];
    if (dfs_timer[to] == -1) {
      dfs(to);
    }
    if (vis[to]) {
      dfs_min[x] = min(dfs_min[to], dfs_min[x]);
    }
  }
  if (dfs_min[x] == dfs_timer[x]) {
    while (!s.empty()) {
      int v = s.top();
      s.pop();
      vis[v] = 0;
      sz[c]++;
      inScc[v] = c;
      if (v == x) {
        break;
      }
    }
    c++;
  }
}
long long mod = 1e9 + 7;
void solve() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    G[i].push_back(a[i]);
  }
  memset(dfs_timer, -1, sizeof(dfs_timer));
  for (int i = 1; i <= n; i++) {
    if (dfs_timer[i] == -1) {
      dfs(i);
    }
  }
  c--;
  long long ans = 1;
  for (int i = 1; i <= c; i++) {
    if (sz[i] == 1) {
      continue;
    }
    long long val = mpow(2, sz[i], mod);
    val = (val - 2 + mod) % mod;
    ans = (ans * val) % mod;
  }
  for (int i = 1; i <= n; i++) {
    if (inScc[a[i]] != inScc[i]) {
      ans = (ans * (2ll)) % mod;
    }
  }
  cout << ans;
}
int main() {
  int t = 1;
  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}
