#include <bits/stdc++.h>
using namespace std;
const int Maxn = 200010;
const int inf = 2147483647;
const int mod = 1000000007;
int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar();
  return x * f;
}
int n, fa[Maxn], c1[Maxn], c2[Maxn];
bool mark[Maxn], flag[Maxn];
int findfa(int x) { return ((fa[x] == x) ? x : fa[x] = findfa(fa[x])); }
int main() {
  n = read();
  for (int i = 1; i <= (n << 1); i++)
    fa[i] = i, c1[i] = 1, c2[i] = 0, flag[i] = false;
  for (int i = 1; i <= n; i++) {
    int x = read(), y = read();
    mark[x] = mark[y] = true;
    int fx = findfa(x), fy = findfa(y);
    if (fx != fy) {
      flag[fy] |= flag[fx];
      fa[fx] = fy;
      c1[fy] += c1[fx];
      c2[fy] += c2[fx] + 1;
    } else {
      c2[fy]++;
      if (x == y) flag[fy] = true;
    }
  }
  int ans = 1;
  for (int i = 1; i <= (n << 1); i++)
    if (mark[i] && findfa(i) == i) {
      if (c2[i] > c1[i]) {
        ans = 0;
        break;
      }
      if (c2[i] + 1 == c1[i])
        ans = (long long)ans * c1[i] % mod;
      else if (!flag[i])
        ans = (long long)ans * 2LL % mod;
    }
  printf("%d", ans);
}
