#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
int deps[200005], vis[200005];
int g[200005];
long long pow(int a, int b, int mod) {
  long long ret = 1;
  long long A = a;
  while (b) {
    if (b & 1) ret = ret * A % mod;
    A = A * A % mod;
    b >>= 1;
  }
  return ret;
}
long long ans = 1;
int cnt = 0;
void dfs(int cur, int parent, int dep) {
  deps[cur] = dep;
  vis[cur] = parent;
  if (!vis[g[cur]]) {
    dfs(g[cur], parent, dep + 1);
  } else {
    if (vis[g[cur]] == parent) {
      cnt += deps[cur] - deps[g[cur]] + 1;
      ans = ans * (pow(2, deps[cur] - deps[g[cur]] + 1, 1000000007) - 2) %
            1000000007;
    }
  }
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &g[i]);
  }
  for (int i = 1; i <= n; i++) {
    if (!vis[i]) {
      dfs(i, i, 0);
    }
  }
  ans = ans * pow(2, (n - cnt), 1000000007) % 1000000007;
  printf("%lld\n", ans);
}
