#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 10;
const long long MOD = 1e9 + 7;
int par[MAXN + MAXN], sz[MAXN + MAXN], kind[MAXN + MAXN];
int N;
int u_find(int node) {
  return (par[node] == node) ? node : par[node] = u_find(par[node]);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  for (int i = 0; i <= 2 * N; i++) par[i] = i, sz[i] = 1;
  int u, v;
  for (int i = 0; i < N; i++) {
    cin >> u >> v;
    if (u == v) {
      u = u_find(u);
      kind[u] |= 2;
      continue;
    }
    u = u_find(u);
    v = u_find(v);
    if (u != v) {
      par[u] = v;
      sz[v] += sz[u];
      kind[v] |= kind[u];
    } else
      kind[u] |= 1;
  }
  long long ans = 1;
  for (int i = 1; i <= 2 * N; i++)
    if (u_find(i) == i) {
      if (kind[i] == 1)
        ans = (ans * 2) % MOD;
      else if (kind[i] == 0)
        ans = (ans * sz[i]) % MOD;
    }
  cout << ans;
  return 0;
}
