#include <bits/stdc++.h>
using namespace std;
const int maxn = 1000000;
const long long mod = 1000000007;
int Fir[maxn], Nxt[maxn], Too[maxn], tot = 1;
int n, fa[maxn];
bool vis[maxn], ins[maxn];
int cir[maxn], ed[maxn], cnt;
inline long long power(long long a, long long b) {
  long long rat = 1;
  while (b) {
    if (b & 1) rat = rat * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return rat % mod;
}
inline void add(int a, int b) {
  Too[++tot] = b;
  Nxt[tot] = Fir[a];
  Fir[a] = tot;
}
inline void dfs_c(int u, int from) {
  vis[u] = ins[u] = 1;
  for (int i = Fir[u]; i; i = Nxt[i]) {
    if (i == (from ^ 1)) continue;
    int v = Too[i];
    fa[v] = u;
    if (!vis[v])
      dfs_c(v, i);
    else if (ins[v]) {
      ++cnt;
      ed[cnt] = ed[cnt - 1];
      for (int k = u; k != v; k = fa[k]) cir[++ed[cnt]] = k;
      cir[++ed[cnt]] = v;
    }
  }
  ins[u] = 0;
}
inline int dfs_s(int u) {
  vis[u] = 1;
  int siz = 0;
  for (int i = Fir[u]; i; i = Nxt[i]) {
    int v = Too[i];
    if (!vis[v]) siz += dfs_s(v);
  }
  return siz + 1;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    int a;
    scanf("%d", &a);
    add(i, a);
    add(a, i);
  }
  for (int i = 1; i <= n; ++i)
    if (!vis[i]) dfs_c(i, -1);
  memset(vis, 0, sizeof(vis));
  for (int i = 1; i <= ed[cnt]; ++i) vis[cir[i]] = 1;
  long long ans = 1;
  for (int i = 1; i <= cnt; ++i) {
    int siz = 0;
    for (int j = ed[i - 1] + 1; j <= ed[i]; ++j) siz += dfs_s(cir[j]);
    ans = ans * power(2, siz - ed[i] + ed[i - 1]) % mod *
          (power(2, ed[i] - ed[i - 1]) - 2) % mod;
  }
  printf("%lld", ans);
  return 0;
}
