#include <bits/stdc++.h>
using namespace std;
int read() {
  char c;
  int sum = 0, f = 1;
  c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    sum = sum * 10 + c - '0';
    c = getchar();
  }
  return sum * f;
}
int n, x, y;
int fa[200005], size[200005], can[200005], huan[200005];
long long ans = 1;
void init() {
  for (int i = 0; i <= 2 * n; i++) fa[i] = i, size[i] = 1;
}
int getfa(int x) { return fa[x] == x ? x : fa[x] = getfa(fa[x]); }
int main() {
  n = read();
  init();
  for (int i = 1; i <= n; i++) {
    x = read();
    y = read();
    if (x == y) can[getfa(x)] = 1;
    int u = getfa(x), v = getfa(y);
    if (u != v) {
      fa[u] = v;
      size[v] += size[u];
      can[v] |= can[u];
    } else
      huan[u] = 1;
  }
  for (int i = 1; i <= 2 * n; i++)
    if (getfa(i) == i) {
      if (can[i]) continue;
      if (huan[i])
        ans = ans * 2 % 1000000007;
      else
        ans = ans * (long long)size[i] % 1000000007;
    }
  printf("%lld\n", ans);
}
