#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int a, long long int b) {
  return b ? gcd(b, a % b) : a;
}
long long int lcm(long long int a, long long int b) {
  return a * b / gcd(a, b);
}
const int N = 2e5 + 10;
const int MOD = 1e9 + 7;
int n, col[N], cntr, d[N];
bool f;
long long int pw[N];
vector<int> ad[N];
void dfs(int v) {
  col[v] = 1;
  for (auto u : ad[v]) {
    if (col[v] == 2) continue;
    if (col[u] == 1) {
      cntr = d[v] - d[u] + 1;
      break;
    }
    if (!col[u]) {
      d[u] = d[v] + 1;
      dfs(u);
    }
  }
  col[v] = 2;
}
int main() {
  scanf("%d", &n);
  pw[0] = 1;
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    ad[i].push_back(x);
    pw[i] = (pw[i - 1] % MOD * 2 % MOD) % MOD;
  }
  int cn = 0;
  long long int ans = 1;
  for (int i = 1; i <= n; i++)
    if (!col[i]) {
      cntr = 0;
      dfs(i);
      if (cntr) {
        cn += cntr;
        ans = (ans % MOD * (pw[cntr] % MOD - 2 % MOD) % MOD) % MOD;
      }
    }
  int rem = n - cn;
  ans = (ans % MOD * pw[rem] % MOD) % MOD;
  printf("%lld\n", ans);
  return 0;
}
