#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10, mod = 1e9 + 7;
struct Edge {
  int to, nxt;
} e[maxn];
int head[maxn], w[maxn], top = 0, mi2[maxn], dep[maxn], cnt = 0, n, vis[maxn];
int mul(int x, int y) { return 1ll * x * y % mod; }
void addedge(int x, int y) {
  ++cnt;
  e[cnt].to = y;
  e[cnt].nxt = head[x];
  head[x] = cnt;
}
void dfs(int x, int d) {
  vis[x] = 1;
  dep[x] = d;
  for (int i = head[x]; i; i = e[i].nxt) {
    int y = e[i].to;
    if (!vis[y])
      dfs(y, d + 1);
    else if (vis[y] == 1)
      w[++top] = dep[x] - dep[y] + 1;
  }
  vis[x] = 2;
}
inline long long quickPow(long long a, long long b) {
  long long ans = 1;
  while (b) {
    if (b & 1) ans = ans * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return ans;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    int x;
    scanf("%d", &x);
    addedge(i, x);
  }
  for (int i = 1; i <= n; ++i)
    if (!dep[i]) dfs(i, 1);
  long long sum = 0, res = 1;
  for (int i = 1; i <= top; ++i) {
    sum += w[i];
    res = (res * (quickPow(2, w[i]) - 2 + mod) % mod + mod) % mod;
  }
  res = (res * quickPow(2, n - sum) % mod + mod) % mod;
  printf("%d\n", res);
  return 0;
}
