#include <bits/stdc++.h>
using namespace std;
bool hs(int mask, int i) { return mask & (1 << i); }
int dp[300000];
void dfs2(vector<int> &adj, int i, int &c) {
  c++;
  dp[i] = 3;
  int u = adj[i];
  if (dp[u] == 3) {
    return;
  }
  dfs2(adj, u, c);
}
void dfs(vector<int> &adj, int i, int &c) {
  dp[i] = 2;
  int u = adj[i];
  if (dp[u] == 0) {
    dfs(adj, u, c);
  } else if (dp[u] == 2) {
    dfs2(adj, i, c);
  }
  dp[i] = 1;
}
int main() {
  int n, t;
  cin >> n;
  t = n;
  long long m = 1000000007;
  vector<int> adj;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    a--;
    adj.push_back(a);
  }
  memset(dp, 0, sizeof(dp));
  long long ans = 1, k = 1;
  vector<long long> v(200005);
  for (int i = 0; i < v.size(); i++) {
    v[i] = k;
    k = k * 2ll;
    k = k % m;
  }
  for (int i = 0; i < n; i++) {
    int c = 0;
    if (dp[i] == 0) {
      dfs(adj, i, c);
      t = t - c;
      if (c > 1) {
        ans = ans * (v[c] - 2ll);
        ans = ans % m;
      }
    }
  }
  ans = ans * v[t];
  ans = ans % m;
  cout << ans << endl;
}
