#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-6;
const int INF = 2e9;
const long long LNF = 9e18;
const int mod = 1e9 + 7;
const int maxn = 2e5 + 10;
int n, a[maxn];
int vis[maxn], group[maxn];
long long ans;
long long fun(long long x, long long n) {
  long long ret = 1;
  while (n > 0) {
    if (n & 1) ret = (ret * x) % mod;
    x = (x * x) % mod;
    n >>= 1;
  }
  return ret;
}
void dfs(int k, int id, int cnt) {
  vis[k] = cnt;
  group[k] = id;
  if (group[a[k]]) {
    if (group[a[k]] == id) {
      ans *= fun(2, cnt - vis[a[k]] + 1) - 2, ans %= mod;
      ans *= fun(2, vis[a[k]] - 1), ans %= mod;
    } else
      ans *= fun(2, cnt), ans %= mod;
  } else
    dfs(a[k], id, cnt + 1);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  ans = 1;
  memset((vis), (0), sizeof(vis));
  memset((group), (0), sizeof(group));
  for (int i = 1; i <= n; i++)
    if (!vis[i]) dfs(i, i, 1);
  printf("%lld\n", ans);
}
