#include <bits/stdc++.h>
using namespace std;
const int N = 200010, mod = 1000000007;
int n, a[N], vis[N], que[N], hd, tl, in[N], f[N];
long long ans = 1;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    in[a[i]]++;
    f[i] = 1;
  }
  for (int i = 1; i <= n; i++)
    if (!in[i]) que[++tl] = i;
  while (hd < tl) {
    int x = que[++hd], y = a[x];
    vis[x] = 1;
    f[y] += f[x];
    in[y]--;
    if (!in[y]) que[++tl] = y;
  }
  for (int i = 1; i <= n; i++)
    if (!vis[i]) {
      int k = i, num = 0, sum = 0, p1 = 1, p2 = 1;
      while (!vis[k]) vis[k] = 1, k = a[k], num++, sum += f[k];
      for (int i = 1; i <= sum; i++) (p1 *= 2) %= mod;
      for (int i = 1; i <= sum - num + 1; i++) (p2 *= 2) %= mod;
      (ans *= (p1 - p2 + mod)) %= mod;
    }
  printf("%I64d\n", ans);
  return 0;
}
