#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -f;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
template <typename T>
inline void Max(T &a, T &b) {
  if (a < b) a = b;
}
template <typename T>
inline void Min(T &a, T &b) {
  if (a > b) a = b;
}
const double pi = (double)acos(-1.0);
const double eps = (double)1e-10;
const int INF = (int)0x3f3f3f3f;
const int MOD = (int)1e9 + 7;
const int MAXN = (int)2e5 + 10;
const int MAXM = (int)1e5 + 10;
int n;
int p[MAXN];
int cnt[MAXN];
int mark[MAXN];
int Find(int x) { return x == p[x] ? x : p[x] = Find(p[x]); }
void work() {
  scanf("%d", &n);
  for (int i = 1; i <= n * 2; i++) p[i] = i;
  for (int i = 1; i <= n; i++) {
    int x, y, u, v;
    u = read();
    v = read();
    x = Find(u);
    y = Find(v);
    if (u == v) {
      mark[x] = 1;
    } else if (x == y) {
      mark[x] = 2;
    } else {
      p[x] = y;
    }
  }
  for (int i = 1; i <= n * 2; i++) {
    int t = Find(i);
    if (mark[i] == 1) mark[t] = cnt[t] = 1;
    if (mark[i] == 2) mark[t] = cnt[t] = 2;
  }
  for (int i = 1; i <= 2 * n; i++) {
    int x = Find(i);
    if (mark[x]) continue;
    cnt[x]++;
  }
  long long ans = 1;
  for (int i = 1; i <= n * 2; i++)
    if (p[i] == i) {
      ans = ans * cnt[i] % MOD;
    }
  printf("%I64d\n", ans);
}
int main() {
  work();
  return 0;
}
