#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
const long long mod = 1e9 + 7;
long long quickpow(long long x, long long y) {
  long long result = 1;
  x %= mod;
  while (y > 0) {
    if (y & 1) {
      result = (result * x) % mod;
    }
    x = (x * x) % mod;
    y = y >> 1;
  }
  return result;
}
struct edge {
  int v;
  int next;
} e[maxn * 2];
int head[maxn];
int total = 0;
void addedge(int u, int v) {
  total++;
  e[total].v = v;
  e[total].next = head[u];
  head[u] = total;
}
int vis[maxn];
int maxans = 0;
int node = 0;
int dp[maxn];
void dfs(int u, int fa) {
  node++;
  for (int i = head[u]; i; i = e[i].next) {
    int v = e[i].v;
    if (v == fa) continue;
    if (!vis[v]) {
      vis[v] = 1;
      dp[v] = dp[u] + 1;
      dfs(v, u);
    } else {
      maxans = max(maxans, dp[u] + 1 - dp[v]);
    }
  }
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int temp;
    scanf("%d", &temp);
    addedge(i, temp);
    addedge(temp, i);
  }
  long long ans = 1;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      maxans = 1;
      node = 0;
      vis[i] = 1;
      dfs(i, -1);
      if (maxans == 1) maxans = 2;
      if (maxans > node) continue;
      ans = (ans *
             (quickpow(2, node - maxans) * (quickpow(2, maxans) + mod - 2) %
              mod) %
             mod) %
            mod;
    }
  }
  printf("%lld", ans % mod);
}
