#include <bits/stdc++.h>
using namespace std;
int n;
int roads[200005];
int visitCounts[200005];
int groups[200005];
int gcts[200005][2];
int fillIn(int pos, int group, bool add) {
  while (visitCounts[pos] != 2) {
    if (visitCounts[pos] == 1 && add) {
      if (groups[pos] != group && group != 0) {
        break;
      }
    }
    groups[pos] = group;
    if (add) {
      visitCounts[pos]++;
    }
    pos = roads[pos];
  }
  return groups[pos];
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> roads[i];
    roads[i]--;
  }
  int group = 1;
  for (int i = 0; i < n; i++) {
    if (visitCounts[i] == 0) {
      int egroup = fillIn(i, group, true);
      if (egroup != group) {
        fillIn(i, egroup, false);
        group--;
      }
    }
    group++;
  }
  for (int i = 0; i < n; i++) {
    gcts[groups[i]][visitCounts[i] - 1]++;
  }
  long long mod = 1e9 + 7;
  long long ways = 1;
  for (int i = 1; i < group; i++) {
    long long mult = 1;
    for (int j = 0; j < gcts[i][1]; j++) {
      mult *= 2;
      mult %= mod;
    }
    mult -= 2;
    for (int j = 0; j < gcts[i][0]; j++) {
      mult *= 2;
      mult %= mod;
    }
    if (mult > 0) {
      ways *= mult;
    }
    ways %= mod;
  }
  cout << ways << endl;
  return 0;
}
