#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 5000;
const long double eps = (1e-12);
const int logn = 21;
const double Pi = acos(-1.0);
const long long pp = 1000000000 + 7;
int lowbit(int x) { return x & (-x); }
const long long INF = 1e9;
long long fa[MAXN];
bool used[MAXN];
long long sz[MAXN];
long long zijiao[MAXN];
long long find(long long x) {
  if (x == fa[x]) return x;
  return fa[x] = find(fa[x]);
}
void merge(long long x, long long y) {
  if (x == y) {
    x = find(x);
    zijiao[x] = 1;
  }
  x = find(x);
  y = find(y);
  if (x == y) {
    used[x] = 1;
  } else {
    fa[y] = x;
    sz[x] += sz[y];
    sz[y] = 0;
    if (used[y]) used[x] = 1;
    if (zijiao[y]) zijiao[x] = 1;
  }
}
signed main() {
  long long n;
  scanf("%lld", &n);
  for (long long i = 1; i <= 2 * n; i++) {
    fa[i] = i;
    sz[i] = 1;
  }
  for (long long i = 1; i <= n; i++) {
    long long x, y;
    scanf("%lld%lld", &x, &y);
    merge(x, y);
  }
  long long ans = 1;
  for (long long i = 1; i <= 2 * n; i++) {
    long long f = find(i);
    if (f == i && sz[f] > 1) {
      if (zijiao[f]) continue;
      if (used[f] == 1)
        ans *= 2, ans %= pp;
      else {
        ans *= sz[f], ans %= pp;
      }
    }
  }
  printf("%lld\n", ans);
  return 0;
}
