#include <bits/stdc++.h>
using namespace std;
constexpr long long maxn = 2e5 + 5, inf = 0x3f3f3f3f, mod = 1e9 + 7;
long long n, m, du[maxn], vis[maxn], f[maxn];
long long res, ans = 1;
vector<long long> e[maxn];
map<pair<long long, long long>, long long> mp;
inline long long ksm(long long a, long long b) {
  long long k = 1;
  while (b) {
    if (b & 1) k = k * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return k;
}
void topo() {
  queue<long long> q;
  for (long long i = 1; i <= n; i++) {
    if (du[i] == 1) {
      q.push(i);
      vis[i] = i;
    }
  }
  while (!q.empty()) {
    long long u = q.front();
    q.pop();
    for (auto v : e[u]) {
      du[v]--;
      if (du[v] <= 1 && !vis[v]) {
        vis[v] = v;
        q.push(v);
      }
    }
  }
}
void dfs(long long u, long long fa) {
  vis[u] = fa;
  f[fa]++;
  for (auto v : e[u]) {
    if (!vis[v] && du[v] > 1) {
      dfs(v, fa);
    }
  }
}
signed main() {
  cin >> n;
  long long flag = 0;
  for (long long i = 1; i <= n; i++) {
    long long x;
    cin >> x;
    e[i].emplace_back(x);
    e[x].emplace_back(i);
    du[i]++;
    du[x]++;
  }
  topo();
  for (long long i = 1; i <= n; i++) {
    if (!vis[i] && du[i] > 1) {
      dfs(i, i);
    }
  }
  for (long long i = 1; i <= n; i++) {
    if (vis[i] == i && f[i] == 0) {
      res++;
    } else if (vis[i] == i && f[i] > 1) {
      ans = ans * (ksm(2, f[i]) - 2) % mod;
    }
  }
  cout << ans * ksm(2, res) % mod;
}
