#include <bits/stdc++.h>
using namespace std;
const int MAXE = 200005;
const int MOD = 1e9 + 7;
struct Edge {
  int from, to;
  int next;
} edge[MAXE * 2];
int head[MAXE];
int cnt;
void addedge(int u, int v) {
  edge[cnt].next = head[u];
  edge[cnt].from = u;
  edge[cnt].to = v;
  head[u] = cnt++;
  edge[cnt].next = head[v];
  edge[cnt].from = u;
  edge[cnt].to = u;
  head[v] = cnt++;
}
int n;
int tmp;
long long fac[MAXE];
int dep[MAXE];
bool vis[MAXE];
long long ans;
int sigh;
void dfs(int u, int p, int depth) {
  if (vis[u]) {
    if (sigh == 0) sigh = depth - dep[u];
    return;
  }
  vis[u] = true;
  dep[u] = depth;
  for (int i = head[u]; i != -1; i = edge[i].next) {
    if (edge[i].from == p) continue;
    dfs(edge[i].to, edge[i].from, depth + 1);
  }
}
int main() {
  fac[0] = 1;
  for (int i = 1; i <= MAXE; i++) fac[i] = fac[i - 1] * 2 % MOD;
  while (scanf("%d", &n) != EOF) {
    int sum = 0;
    ans = 1;
    cnt = 0;
    memset(vis, false, sizeof vis);
    memset(dep, 0, sizeof dep);
    memset(head, -1, sizeof head);
    for (int i = 1; i <= n; i++) {
      scanf("%d", &tmp);
      addedge(i, tmp);
    }
    for (int i = 1; i <= n; i++) {
      if (vis[i]) continue;
      sigh = 0;
      dfs(i, 0, 1);
      sum += sigh;
      ans = ans * (fac[sigh] - 2) % MOD;
    }
    printf("%lld\n", ans * fac[n - sum] % MOD);
  }
  return 0;
}
