#include <bits/stdc++.h>
using namespace std;
const int maxn = 2 * 100010;
const int mod = 1e9 + 7;
int pre[maxn];
int sz[maxn];
bool loop[maxn], cycle[maxn];
void init() {
  for (int i = 0; i < maxn; i++) {
    pre[i] = i;
    sz[i] = 1;
    loop[i] = false;
    cycle[i] = false;
  }
}
int findF(int x) {
  if (pre[x] == x) return x;
  return pre[x] = findF(pre[x]);
}
int main() {
  int n;
  while (~scanf("%d", &n)) {
    init();
    for (int i = 0; i < n; i++) {
      int x, y;
      scanf("%d%d", &x, &y);
      if (x == y) loop[findF(x)] = true;
      int u = findF(x), v = findF(y);
      if (u != v) {
        sz[v] += sz[u];
        pre[u] = v;
        loop[v] |= loop[u];
      } else
        cycle[u] = true;
    }
    long long ans = 1;
    for (int i = 1; i <= 2 * n; i++) {
      if (pre[i] == i) {
        if (loop[i]) continue;
        if (cycle[i])
          ans = ans * 2 % mod;
        else
          ans = ans * sz[i] % mod;
      }
    }
    printf("%I64d\n", ans);
  }
  return 0;
}
