#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
const int mo = 1e9 + 7;
vector<int> e[maxn];
int low[maxn];
int vis[maxn];
int c;
long long ans, sum, m;
void dfs(int u, int fa, int d) {
  int v, i, k = 0;
  if (d <= low[u]) return;
  if (low[u] != -1) {
    c = d - low[u];
    m -= c;
    sum = 1;
    for (i = 1; i <= c; i++) sum = (sum * 2) % mo;
    sum = (sum - 2 + mo) % mo;
    ans = (ans * sum) % mo;
    return;
  }
  low[u] = d;
  for (i = 0; i < e[u].size(); i++) {
    v = e[u][i];
    if (v == fa) {
      k++;
      continue;
    }
    dfs(v, u, d + 1);
  }
  if (k == 2) dfs(fa, u, d + 1);
}
int main() {
  int n, i, x;
  scanf("%d", &n);
  m = n;
  for (i = 1; i <= n; i++) e[i].clear();
  for (i = 1; i <= n; i++) {
    scanf("%d", &x);
    e[i].push_back(x);
    e[x].push_back(i);
  }
  ans = 1;
  memset(low, -1, sizeof(low));
  for (i = 1; i <= n; i++) {
    if (low[i] == -1) dfs(i, 0, 1);
  }
  for (i = 1; i <= m; i++) ans = (ans * 2) % mo;
  printf("%I64d\n", ans);
  return 0;
}
