#include <bits/stdc++.h>
using namespace std;
long long par[200010], vis[200010];
int main() {
  long long n, i, c, j;
  long long curr = 0;
  cin >> n;
  long long power[200010] = {0};
  power[0] = 1;
  for (i = 1; i <= n; i++) {
    power[i] = (power[i - 1] << 1) % 1000000007;
  }
  for (i = 1; i <= n; i++) {
    cin >> par[i];
  }
  long long ans = 1;
  long long k = n;
  for (i = 1; i <= n; i++) {
    if (!vis[i]) {
      long long final = curr;
      j = i;
      while (!vis[j]) {
        vis[j] = ++curr;
        j = par[j];
      }
      if (vis[j] > final) {
        c = curr - vis[j] + 1;
        ans = 1ll * ans * (power[c] - 2 + 1000000007) % 1000000007;
        k -= c;
      }
    }
  }
  ans = 1ll * ans * power[k] % 1000000007;
  cout << ans;
}
