#include <bits/stdc++.h>
using namespace std;
const int mo = 1e9 + 7, maxn = 200005;
int n, a[maxn], low[maxn], dfn[maxn], st[maxn], ans, top, tot, p[maxn];
bool visit[maxn], bz[maxn], flag;
char c;
int read() {
  for (c = getchar(); c < '0' || c > '9'; c = getchar())
    ;
  int x = c - 48;
  for (c = getchar(); c >= '0' && c <= '9'; c = getchar()) x = x * 10 + c - 48;
  return x;
}
void dfs(int x) {
  visit[x] = bz[x] = 1;
  tot++;
  if (visit[a[x]]) {
    if (bz[a[x]]) {
      int cnt = 1;
      for (int i = a[x]; i != x; i = a[i]) cnt++;
      ans = (long long)ans * (p[cnt] - 2) % mo;
      tot -= cnt;
    }
  } else
    dfs(a[x]);
  bz[x] = 0;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  p[0] = 1;
  for (int i = 1; i <= n; i++) p[i] = p[i - 1] * 2 % mo;
  ans = 1;
  for (int i = 1; i <= n; i++)
    if (!visit[i]) {
      tot = 0;
      dfs(i);
      top += tot;
    }
  ans = (long long)ans * p[top] % mo;
  printf("%d\n", ans);
  return 0;
}
