#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1000000007;
vector<int> g1[200020], g2[200020];
int N, compo[200020], vis[200020], vis2[200020], cnt = 1, xua;
vector<int> mark;
void dfs(int u, int c) {
  if (vis[u]) return;
  vis[u] = 1, compo[u] = c;
  for (int i = 0; i < g1[u].size(); i++) dfs(g1[u][i], c);
  for (int i = 0; i < g2[u].size(); i++) dfs(g2[u][i], c);
}
void dfs2(int u) {
  if (vis[u]) return;
  vis[u] = 1;
  xua++;
  for (int i = 0; i < g2[u].size(); i++) dfs2(g2[u][i]);
}
int main() {
  scanf("%d", &N);
  int u, v;
  for (int i = 0; i < N; i++) {
    scanf("%d %d", &u, &v);
    if (u == v) mark.push_back(u);
    g1[u].push_back(v);
    g2[v].push_back(u);
  }
  for (int i = 1; i < 2 * N + 1; i++) {
    if (!vis[i]) dfs(i, cnt++);
  }
  memset(vis, 0, sizeof vis);
  long long int ans = 1;
  for (int i = 1; i < 2 * N + 1; i++) {
    if (!vis[i] && g1[i].size() == 0) {
      xua = 0;
      dfs2(i);
      vis2[compo[i]] = 1;
      ans *= (xua);
      if (ans >= mod) ans %= mod;
    }
  }
  for (int i = 0; i < mark.size(); i++) vis2[compo[mark[i]]] = 1;
  for (int i = 1; i < cnt; i++) {
    if (vis2[i]) continue;
    ans *= (2);
    if (ans >= mod) ans %= mod;
  }
  cout << ans << '\n';
  return 0;
}
