#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200005;
long long int N;
long long int directed[MAXN];
long long int cycles[MAXN];
int visited[MAXN];
int cyclecnt;
void dfs(int u) {
  visited[u] = 1;
  int v = directed[u];
  if (visited[v] == 0) {
    dfs(v);
  } else if (visited[v] == 1) {
    cyclecnt++;
    long long int num = 1;
    long long int x = directed[u];
    while (x != u) {
      x = directed[x];
      num++;
    }
    cycles[cyclecnt] = num;
  }
  visited[u] = 2;
}
long long int MODEXPO(long long int pp) {
  long long int n = pp;
  long long int res1 = 1;
  long long int t = 2;
  while (n) {
    if (n & 1) {
      res1 = (res1 * t) % 1000000007;
    }
    t = (t * t) % 1000000007;
    n >>= 1;
  }
  return res1;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> directed[i];
    visited[i] = 0;
  }
  cyclecnt = 0;
  for (int i = 1; i <= N; i++) {
    if (visited[i] == 0) {
      dfs(i);
    }
  }
  long long int n = N;
  long long int ans = 1;
  for (int i = 1; i <= cyclecnt; i++) {
    n -= cycles[i];
    ans = (ans * (MODEXPO(cycles[i]) - 2 + 1000000007)) % 1000000007;
  }
  ans = (ans * MODEXPO(n)) % 1000000007;
  cout << ans << endl;
}
