#include <bits/stdc++.h>
using namespace std;
constexpr long long maxn = 2e5 + 5, inf = 0x3f3f3f3f, mod = 1e9 + 7;
long long f[maxn], n, m, s[maxn], c[maxn], ans = 1, h[maxn];
long long find(long long x) {
  if (f[x] == x)
    return x;
  else
    return f[x] = find(f[x]);
}
inline void hb(long long x, long long y) {
  long long fx = find(x);
  long long fy = find(y);
  if (fx != fy) {
    h[fy] |= h[fx];
    s[fy] += s[fx];
    c[fy] += c[fx] + 1;
    f[fx] = fy;
  } else if (fx == fy) {
    c[fy]++;
  }
}
signed main() {
  cin >> n;
  for (long long i = 1; i <= 2 * n; i++) {
    f[i] = i;
    s[i] = 1;
  }
  for (long long i = 1; i <= n; i++) {
    long long u, v;
    cin >> u >> v;
    if (u == v) h[u] = h[v] = 1;
    hb(u, v);
  }
  for (long long i = 1; i <= 2 * n; i++) {
    if (f[i] == i && s[i] != 1 && !h[i]) {
      if (s[i] == c[i]) {
        ans = ans * 2 % mod;
      } else if (s[i] == c[i] + 1) {
        ans = ans * s[i] % mod;
      }
    }
  }
  cout << ans % mod;
}
