#include <bits/stdc++.h>
struct graph {
  int v;
  struct graph *next;
} * arr[250000], *remem[250000], *arr_rev[250000], *remem_rev[250000];
int visited[250000];
int b_set[250000];
int n;
int cnt;
void insert_edge(int v, int v1, int key) {
  struct graph *t = (struct graph *)malloc(sizeof(struct graph));
  t->v = v1;
  t->next = NULL;
  if (key && !arr[v]) {
    arr[v] = t;
    remem[v] = t;
    return;
  }
  if (!key && !arr_rev[v]) {
    arr_rev[v] = t;
    remem_rev[v] = t;
    return;
  }
  if (key) {
    remem[v]->next = t;
    remem[v] = t;
  } else {
    remem_rev[v]->next = t;
    remem_rev[v] = t;
  }
}
void dfs(int x) {
  if (visited[x]) return;
  visited[x] = 1;
  struct graph *sweep = arr[x];
  while (sweep) {
    dfs(sweep->v);
    sweep = sweep->next;
  }
  b_set[n++] = x;
}
void dfs1(int x) {
  if (visited[x]) return;
  visited[x] = 1;
  cnt++;
  struct graph *sweep = arr_rev[x];
  while (sweep) {
    dfs1(sweep->v);
    sweep = sweep->next;
  }
}
long long int compute(long long int a, long long int b) {
  long long int res = 1;
  while (b) {
    if (b % 2) {
      res = (res * a) % ((long long int)1e9 + 7);
    }
    a = (a * a) % ((long long int)1e9 + 7);
    b /= 2;
  }
  return res;
}
int main() {
  int a, b, c, d;
  scanf("%d", &a);
  for (int i = 0; i < a; i++) {
    int in;
    scanf("%d", &in);
    insert_edge(i + 1, in, 1);
    insert_edge(in, i + 1, 0);
  }
  for (int i = 1; i <= a; i++) {
    if (!visited[i]) dfs(i);
  }
  memset(visited, 0, sizeof(visited));
  long long int ans = 1;
  for (int i = n - 1; i >= 0; i--) {
    if (!visited[b_set[i]]) {
      cnt = 0;
      dfs1(b_set[i]);
      long long int go;
      if (cnt == 1 || cnt == 2)
        go = 2;
      else
        go = compute(2, (long long int)cnt) - 2;
      ans = (ans * go) % ((long long int)1e9 + 7);
    }
  }
  printf("%lld", ans);
}
