#include <bits/stdc++.h>
const int N = 200005;
const int mod = 1e9 + 7;
int n, a[N], ok[N], ans = 1, tim = 0, pw[N], sum = 0;
using namespace std;
void find(int x) {
  int y = tim;
  while (!ok[x]) ok[x] = ++tim, x = a[x];
  if (ok[x] <= y) return;
  y = x;
  int t = 1;
  x = a[x];
  while (x != y) t++, x = a[x];
  sum += t;
  ans = (long long)ans * ((pw[t] - 2 + mod) % mod) % mod;
}
int main() {
  cin >> n;
  int i;
  pw[0] = 1;
  for (i = 1; i <= n; i++) scanf("%d", &a[i]), pw[i] = (pw[i - 1] << 1) % mod;
  for (i = 1; i <= n; i++)
    if (!ok[i]) find(i);
  cout << (long long)ans * pw[n - sum] % mod;
  return 0;
}
