#include <bits/stdc++.h>
using namespace std;
const long long P = 1000000000 + 7;
const int maxn = 2e5 + 10;
struct node {
  int v, id;
};
vector<node> G[maxn];
int n;
long long ans;
int sz1, sz2;
int vis[maxn];
long long a[maxn], fac[maxn], rev[maxn];
void dfs(int u, int f) {
  for (int i = 0; i < (int)G[u].size(); i++) {
    int v = G[u][i].v, id = G[u][i].id;
    if (id == f) continue;
    if (vis[v] && vis[v] != -1) {
      sz2 = vis[u] - vis[v] + 1;
    } else if (!vis[v]) {
      sz1++;
      vis[v] = vis[u] + 1;
      dfs(v, id);
    }
  }
  vis[u] = -1;
}
long long C(int n, int m) { return fac[n] * rev[n - m] % P * rev[m] % P; }
long long solve() {
  if (sz1 == 1) return 1;
  long long ret;
  ret = a[sz1 - sz2];
  if (sz2 == 0) return ret;
  long long tmp = (a[sz2] - 2 + P) % P;
  return ret * tmp % P;
}
long long pow_mod(long long a, long long b) {
  long long ret = 1;
  for (; b; a = a * a % P, b = b / 2) {
    if (b % 2 == 1) ret = (ret * a) % P;
  }
  return ret;
}
void init() {
  a[0] = fac[0] = rev[0] = 1;
  for (int i = 1; i <= n; i++) a[i] = (a[i - 1] * 2) % P;
  for (int i = 1; i <= n; i++) fac[i] = (fac[i - 1] * i) % P;
  for (int i = 1; i <= n; i++) rev[i] = pow_mod(fac[i], P - 2);
}
int main() {
  int v;
  scanf("%d", &n);
  init();
  for (int i = 1; i <= n; i++) {
    scanf("%d", &v);
    G[i].push_back((node){v, i});
    G[v].push_back((node){i, i});
  }
  ans = 1;
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      sz1 = sz2 = 0;
      vis[i] = ++sz1;
      dfs(i, 0);
      ans = ans * solve() % P;
    }
  }
  printf("%lld\n", ans);
  return 0;
}
