#include <bits/stdc++.h>
using namespace std;
const int maxn = 200010;
const int mod = 1e9 + 7;
namespace DSU {
int fa[maxn], cycle[maxn], sz[maxn];
void init() {
  for (int i = 1; i < maxn; i++) fa[i] = i, sz[i] = 1, cycle[i] = 0;
}
int find_set(int x) {
  if (x == fa[x])
    return x;
  else
    return fa[x] = find_set(fa[x]);
}
void union_set(int x, int y) {
  int fx = find_set(x);
  int fy = find_set(y);
  if (fx == fy) {
    cycle[fx] = 1;
  } else {
    fa[fy] = fx;
    sz[fx] += sz[fy];
    cycle[fx] |= cycle[fy];
  }
}
}  // namespace DSU
using namespace DSU;
int main() {
  int n;
  cin >> n;
  init();
  long long ans = 1;
  for (int i = 1; i <= n; i++) {
    int x, y;
    cin >> x >> y;
    if (x == y) {
      cycle[find_set(x)] = 2;
      continue;
    }
    union_set(x, y);
  }
  for (int i = 1; i <= 2 * n; i++) {
    if (find_set(i) == i) {
      if (cycle[i] == 1)
        ans = ans * 2 % mod;
      else {
        if (cycle[i] == 0) ans = ans * sz[i] % mod;
      }
    }
  }
  cout << ans << endl;
  return 0;
}
