#include <bits/stdc++.h>
using namespace std;
const long long p = 1000000007;
long long n, a[200010], br[200010], brojac, suma;
vector<long long> vrh, adj[200010], ciklan, res;
bool vis[200010], cik[200010];
long long ans;
long long stp(long long x, long long b) {
  if (b == 0) {
    return 1;
  }
  long long y = stp(x, b / 2);
  y = (y * y) % p;
  if (b % 2 == 1) {
    y = (y * x) % p;
  }
  return y;
}
void dfs(long long u) {
  for (int i = 0; i < adj[u].size(); i++) {
    if (!vis[adj[u][i]]) {
      vis[adj[u][i]] = true;
      dfs(adj[u][i]);
    }
  }
  return;
}
void ciklus(long long u) {
  if (cik[u]) {
    res.push_back(stp(2, (brojac - br[u])));
    suma -= (brojac - br[u]);
    return;
  }
  br[u] = brojac;
  cik[u] = true;
  brojac++;
  u = a[u];
  ciklus(u);
  return;
}
int main() {
  scanf("%I64d", &n);
  suma = n;
  for (int i = 1; i <= n; i++) {
    scanf("%I64d", &a[i]);
    adj[i].push_back(a[i]);
    adj[a[i]].push_back(i);
  }
  for (int i = 1; i <= n; i++) {
    cik[i] = false;
    vis[i] = false;
  }
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      vrh.push_back(i);
      vis[i] = true;
      dfs(i);
    }
  }
  for (int i = 0; i < vrh.size(); i++) {
    brojac = 0;
    ciklus(vrh[i]);
    res[i] = (res[i] + p - 2) % p;
  }
  ans = stp(2, suma);
  for (int i = 0; i < vrh.size(); i++) {
    ans = (ans * res[i]) % p;
  }
  printf("%I64d", ans);
  return 0;
}
