#include <bits/stdc++.h>
using namespace std;
const int maxm = 2e5 + 10;
int m;
int to[maxm];
int flag[maxm];
int tims;
stack<int> st;
long long mod = 1e9 + 7;
long long po(int n) {
  long long ret = 1;
  long long k = 2;
  while (n) {
    if (n & 1) ret = ret * k % mod;
    k = k * k % mod;
    n /= 2;
  }
  return ret;
}
long long ans = 1;
int others;
void calc(int u) {
  while (!flag[u]) {
    flag[u] = tims;
    st.push(u);
    u = to[u];
  }
  if (flag[u] == tims) {
    int now = 0;
    int top;
    for (;;) {
      now++;
      top = st.top();
      st.pop();
      if (top == u) break;
    }
    ans = ans * ((po(now) - 2 + mod) % mod) % mod;
  }
  while (!st.empty()) {
    st.pop();
    others++;
  }
}
int main() {
  scanf("%d", &m);
  for (int i = 1; i <= m; i++) scanf("%d", &to[i]);
  for (int i = 1; i <= m; i++)
    if (!flag[i]) {
      tims++;
      calc(i);
    }
  ans = ans * po(others) % mod;
  cout << ans << endl;
  return 0;
}
