#include <bits/stdc++.h>
using namespace std;
const int tam = 200000;
long long mod = 1000000007;
vector<int> adl[tam + 10];
vector<int> stck;
int low[tam + 10];
int num[tam + 10];
bool visited[tam + 10];
int cnt = 0;
map<int, vector<int> > strcc;
void scc(int node) {
  low[node] = ++cnt;
  num[node] = low[node];
  stck.push_back(node);
  visited[node] = true;
  for (int i = 0; i < (int)adl[node].size(); ++i) {
    int v = adl[node][i];
    if (num[v] == -1) scc(v);
    if (visited[v]) low[node] = min(low[v], low[node]);
  }
  if (low[node] == num[node]) {
    while (1) {
      int v = stck.back();
      stck.pop_back();
      visited[v] = false;
      strcc[node].push_back(v);
      if (v == node) break;
    }
  }
}
long long multmod(long long a, long long b, long long c) {
  long long x = 0, y = a % c;
  while (b > 0) {
    if (b % 2 == 1) {
      x = (x + y) % c;
    }
    y = (y * 2) % c;
    b /= 2;
  }
  return x % c;
}
long long expmod(long long a, long long b, long long c) {
  long long res = 1;
  long long x = a % c;
  while (b > 0) {
    if (b % 2 == 1) res = multmod(res, x, c);
    x = multmod(x, x, c);
    b /= 2;
  }
  return res;
}
int main() {
  ios::sync_with_stdio(false);
  long long n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    int x;
    cin >> x;
    adl[i].push_back(x);
  }
  memset(num, -1, sizeof num);
  memset(visited, false, sizeof visited);
  for (int i = 1; i <= n; ++i) {
    if (num[i] == -1) scc(i);
  }
  long long a;
  long long b = 0;
  long long c = 1;
  for (auto item : strcc) {
    if ((int)item.second.size() > 1) {
      b += (int)item.second.size();
      c = (c * (expmod(2, (long long)item.second.size(), mod) - 2)) % mod;
    }
  }
  a = n - b;
  cerr << a << " " << b;
  cerr << endl;
  cout << (expmod(2, a, mod) * c) % mod;
  cout << endl;
  return 0;
}
