#include <bits/stdc++.h>
using namespace std;
const int N = 200100, p = 1e9 + 7;
int n, m, ans = 1, x, y, fa[N], si[N];
bool flag[N];
inline int ask(int x) { return fa[x] == x ? x : fa[x] = ask(fa[x]); }
inline void merge(int x, int y) {
  if (x == y) return;
  int u = ask(x), v = ask(y);
  if (u == v) {
    ans = ans * 2 % p;
    return;
  }
  si[u] += si[v];
  fa[v] = u;
}
int main() {
  cin >> n;
  for (int i = 1; i <= 2 * n; ++i) fa[i] = i, si[i] = 1;
  for (int i = 1; i <= n; ++i) scanf("%d%d", &x, &y), flag[x] = 1, merge(y, x);
  for (int i = 1; i <= 2 * n; ++i)
    if (!flag[i]) ans = 1ll * ans * si[i] % p;
  printf("%d", ans);
  return 0;
}
