#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  int a[n + 1];
  for (int i = 1; i < n + 1; i++) cin >> a[i];
  vector<int> b;
  bool vis[n + 1];
  memset(vis, false, sizeof vis);
  for (int i = 1; i < n + 1; i++) {
    if (vis[i]) continue;
    map<int, int> d;
    int c = 1;
    d[i] = c;
    vis[i] = true;
    int nex = a[i];
    bool cs = true;
    while (d[nex] == 0) {
      if (vis[nex]) {
        cs = false;
        break;
      }
      vis[nex] = true;
      d[nex] = ++c;
      nex = a[nex];
    }
    if (cs) b.push_back(c + 1 - d[nex]);
  }
  long long int pwr[n + 1];
  pwr[0] = 1;
  for (int i = 1; i < n + 1; i++) pwr[i] = (pwr[i - 1] * 2) % 1000000007;
  long long int ans = 1;
  for (int e : b) ans = (ans * (pwr[e] - 2)) % 1000000007;
  int rem = n - accumulate(b.begin(), b.end(), 0);
  ans = (ans * pwr[rem]) % 1000000007;
  cout << ans;
}
