#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int t = 0;
  char ch = getchar();
  while (ch < '0' || ch > '9') ch = getchar();
  while (ch <= '9' && ch >= '0') t = t * 10 + ch - '0', ch = getchar();
  return t;
}
const int mod = 1e9 + 7;
const int _ = 3e5 + 5;
int fa[_], n, f[_], siz[_], c[_];
inline int find(int x) {
  if (fa[x] == x) return fa[x];
  return fa[x] = find(fa[x]);
}
int main() {
  n = read();
  for (register int i = 1; i <= n << 1; i++) fa[i] = i, siz[i] = 1;
  for (register int i = 1; i <= n; i++) {
    int a = read(), b = read();
    int fx = find(a), fy = find(b);
    if (a == b) {
      f[a] = 1;
    }
    if (fx != fy) {
      fa[fx] = fy, siz[fy] += siz[fx];
      siz[fx] = 0;
      f[fy] |= f[fx];
    } else
      c[fx] = 1;
  }
  long long ans = 1;
  for (register int i = 1; i <= n << 1; i++)
    if (find(i) == i && !f[i]) {
      ans = (c[i] == 1 ? (ans << 1) % mod : (ans * siz[i]) % mod);
    }
  cout << ans << endl;
}
