#include <bits/stdc++.h>
using namespace std;
long long MOD = 1000000007;
long long modpow(long long a, long long n) {
  a %= MOD;
  if (a == 0) return 0;
  long long r = 1;
  while (n) r = r * ((n % 2) ? a : 1) % MOD, a = a * a % MOD, n >>= 1;
  return r;
}
template <int um>
class UF {
 public:
  vector<int> par;
  UF() {
    par = vector<int>(um, 0);
    for (int i = 0; i < um; i++) par[i] = i;
  }
  int operator[](int x) {
    return par[x] == x ? x : par[x] = operator[](par[x]);
  }
  void operator()(int x, int y) {
    x = operator[](x);
    y = operator[](y);
    if (x != y) par[x] = y;
  }
};
int N;
vector<int> E[201010];
int delnum = 0;
void del() {
  queue<int> que;
  for (int i = 0; i < N; i++)
    if (E[i].size() == 1) que.push(i);
  while (!que.empty()) {
    int i = que.front();
    que.pop();
    int j = E[i][0];
    E[i].erase(find(E[i].begin(), E[i].end(), j));
    E[j].erase(find(E[j].begin(), E[j].end(), i));
    delnum++;
    if (E[j].size() == 1) que.push(j);
  }
}
int cnt[201010];
int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    int a;
    scanf("%d", &a);
    a--;
    E[i].push_back(a);
    E[a].push_back(i);
  }
  del();
  UF<201010> uf;
  for (int i = 0; i < N; i++) cnt[i] = 1;
  for (int i = 0; i < N; i++)
    for (int j : E[i]) {
      if (uf[i] != uf[j]) {
        int c = cnt[uf[i]] + cnt[uf[j]];
        uf(i, j);
        cnt[uf[i]] = c;
      }
    }
  long long ans = 0;
  ans = modpow(2, delnum);
  for (int i = 0; i < N; i++)
    if (uf[i] == i && 0 < E[i].size())
      ans = (ans * (modpow(2, cnt[i]) - 2)) % MOD;
  cout << ans << endl;
}
