#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int fa[200000], sz[200000] = {0}, loop[200000] = {0}, cycle[200000] = {0};
int getf(int x) {
  if (fa[x] == x)
    return x;
  else {
    fa[x] = getf(fa[x]);
    return fa[x];
  }
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i <= 2 * n; i++) fa[i] = i, sz[i] = 1;
  int u, v;
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &u, &v);
    if (u == v)
      loop[u] = 1;
    else {
      int a = getf(u);
      int b = getf(v);
      if (a != b) {
        fa[a] = b;
        sz[b] += sz[a];
        loop[a] |= loop[b];
      } else
        cycle[a] = 1;
    }
  }
  long long ans = 1;
  for (int i = 1; i <= 2 * n; i++) {
    if (getf(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;
}
