#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int N = 1e6 + 3;
int input_graph[N] = {0};
int visited[N] = {0};
long long int ans;
vector<int> cycles;
int cycle_count;
long long int dp[N];
void find_participants(int i) {
  cycles[cycle_count]++;
  visited[i] = 3;
  if (visited[input_graph[i]] == 3) return;
  find_participants(input_graph[i]);
}
void traverse(int i) {
  visited[i] = 2;
  if (visited[input_graph[i]] == 0) {
    traverse(input_graph[i]);
  } else if (visited[input_graph[i]] == 1) {
    visited[i] = 1;
    return;
  } else {
    cycles.push_back(0);
    find_participants(i);
    cycle_count++;
  }
  visited[i] = 1;
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> input_graph[i];
  }
  dp[0] = 1;
  for (int i = 1; i <= n; i++) {
    dp[i] = ((dp[i - 1] * 2) % MOD);
  }
  ans = 1;
  for (int i = 1; i <= n; i++) {
    if (visited[i] == 0) {
      traverse(i);
    }
  }
  long long int cnt = n;
  for (int i = 0; i < cycles.size(); i++) {
    cnt -= cycles[i];
    ans = (ans * (dp[cycles[i]] - 2)) % MOD;
  }
  ans = (ans * (dp[cnt])) % MOD;
  if (ans < 0) ans += MOD;
  cout << ans;
  return 0;
}
