#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
int n;
int fa[1000005], huan[1000005], zihuan[1000005], siz[1000005];
int find(int now) { return fa[now] == now ? now : fa[now] = find(fa[now]); }
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= 2 * n; i++) {
    fa[i] = i;
    siz[i] = 1;
  }
  for (int i = 1; i <= n; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    int x1 = find(x), y1 = find(y);
    if (x == y) zihuan[x1] = 1;
    if (x1 == y1) huan[y1] = 1;
    fa[x1] = y1;
  }
  for (int i = 1; i <= 2 * n; i++) {
    int x = find(i);
    if (x != i) {
      siz[x] += siz[i];
      huan[x] |= huan[i];
      zihuan[x] |= zihuan[i];
    }
  }
  long long ans = 1;
  for (int i = 1; i <= 2 * n; i++) {
    if (find(i) == i) {
      if (zihuan[i])
        continue;
      else if (huan[i])
        ans = (ans * 2) % mod;
      else
        ans = (ans * siz[i]) % mod;
    }
  }
  printf("%I64d\n", ans);
}
