#include <bits/stdc++.h>
using namespace std;
vector<int> v[200005];
vector<pair<int, int> > edges;
int top[200005], seen[200005], k = 0;
long long pot[200005], cont = 0;
long long mod = 1000000007;
void dfs(int x) {
  seen[x] = 1;
  for (auto a : v[x]) {
    if (!seen[a]) dfs(a);
  }
  top[k++] = x;
}
void dfs2(int x) {
  seen[x] = 1;
  cont++;
  for (auto a : v[x]) {
    if (!seen[a]) dfs2(a);
  }
}
int main() {
  int n, x;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &x);
    edges.push_back(make_pair(i, x));
    v[i].push_back(x);
  }
  pot[1] = 2ll;
  for (int i = 2; i <= 200002; i++) {
    pot[i] = (pot[i - 1] * 2ll) % mod;
  }
  for (int i = 1; i <= n; i++) {
    if (!seen[i]) dfs(i);
  }
  for (int i = 1; i <= n; i++) v[i].clear();
  for (auto a : edges) {
    int x = a.first;
    int y = a.second;
    v[y].push_back(x);
  }
  memset(seen, 0, sizeof(seen));
  long long ans = 1ll;
  for (int i = k - 1; i >= 0; i--) {
    cont = 0;
    if (!seen[top[i]]) {
      dfs2(top[i]);
      if (cont == 1)
        ans = (ans * 2ll) % mod;
      else
        ans = (ans * (pot[cont] - 2ll)) % mod;
    }
  }
  printf("%lld\n", ans);
  return 0;
}
