#include <bits/stdc++.h>
using namespace std;
long long n;
const long long N = 2e5 + 2;
vector<vector<long long>> v(N);
long long ans = 1, cycleEdges;
long long counter;
long long state[N], position[N];
long long pwmd(long long n, long long m) {
  long long res = 1;
  while (m > 0) {
    if (m & 1) res = (res % 1000000007 * n % 1000000007) % 1000000007;
    m /= 2;
    n = (n % 1000000007 * n % 1000000007) % 1000000007;
  }
  return res;
}
void dfs(long long x, long long p) {
  if (state[x] == 1) return;
  if (state[x] == 2) {
    cycleEdges += position[p] - position[x] + 1;
    ans = (ans * (pwmd(2, position[p] - position[x] + 1) - 2 + 1000000007) %
           1000000007) %
          1000000007;
    return;
  }
  state[x] = 2;
  position[x] = counter++;
  for (long long i : v[x]) {
    dfs(i, x);
  }
  counter--;
  state[x] = 1;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (long long i = 0; i < n; ++i) {
    long long a;
    cin >> a;
    v[i + 1].push_back(a);
  }
  for (long long i = 1; i <= n; ++i) {
    if (state[i] == 0) {
      dfs(i, -1);
    }
  }
  long long leftEdges = n - cycleEdges;
  ans = (ans * pwmd(2, leftEdges)) % 1000000007;
  cout << ans;
  return 0;
}
