#include <bits/stdc++.h>
using namespace std;
const long long modul = 1000000007;
long long ans_x_times(long long ans, int times) {
  for (int i = 0; i < times; i++) {
    ans = (2 * ans) % modul;
  }
  return ans;
}
long long ans_x_times_minus_two(long long ans, int times) {
  return ((ans_x_times(ans, times) - 2 * ans) % modul + modul) % modul;
}
int main() {
  int n;
  cin >> n;
  long long ans = 1;
  int to[n];
  vector<int> time(n, 0);
  int t = 1;
  for (auto& x : to) {
    cin >> x;
    x--;
  }
  for (int i = 0; i < n; i++) {
    if (time[i] == 0) {
      int tbegin = t;
      time[i] = t++;
      int curr = to[i];
      while (time[curr] == 0) {
        time[curr] = t++;
        curr = to[curr];
      }
      if (time[curr] < tbegin) {
        ans = ans_x_times(ans, t - tbegin);
      } else {
        ans = ans_x_times(ans, time[curr] - tbegin);
        ans = ans_x_times_minus_two(ans, t - time[curr]);
      }
    }
  }
  cout << ans << endl;
}
