#include <bits/stdc++.h>
using namespace std;
const long long MOD = (long long)(1e9 + 7);
long long fast_power(long long num, int p) {
  if (p == 0) return 1ll;
  if (p == 1) return num % MOD;
  long long ret = fast_power(num, p / 2);
  ret = (ret * ret) % MOD;
  if (p & 1) ret = (ret * num) % MOD;
  return ret;
}
vector<int> to;
long long calc(int init, int tot, vector<int>& pos) {
  int ptr = init, cnt = 0;
  while (pos[ptr] < 0) {
    pos[ptr] = cnt++;
    ptr = to[ptr];
  }
  int rem = cnt - pos[ptr];
  long long res = fast_power(2ll, tot);
  res = (res - fast_power(2ll, tot - rem + 1)) % MOD;
  res = (res + MOD) % MOD;
  return res;
}
int main() {
  int n;
  cin >> n;
  to.resize(n + 1);
  vector<vector<int> > g(n + 1, vector<int>());
  for (int i = 1; i <= n; ++i) {
    cin >> to[i];
    g[i].push_back(to[i]);
    g[to[i]].push_back(i);
  }
  vector<int> pos(n + 1, -1);
  vector<bool> visited(n + 1, false);
  long long res = 1;
  for (int i = 1; i <= n; ++i) {
    if (visited[i]) continue;
    queue<int> q;
    q.push(i);
    visited[i] = true;
    int cnt = 0;
    while (!q.empty()) {
      int tp = q.front();
      q.pop();
      ++cnt;
      for (int j = 0; j < (int)g[tp].size(); ++j) {
        int u = g[tp][j];
        if (!visited[u]) {
          visited[u] = true;
          q.push(u);
        }
      }
    }
    res = res * calc(i, cnt, pos);
    res %= MOD;
  }
  cout << res << endl;
  return 0;
}
