#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200010;
const int mod = 1e9 + 7;
int f[MAXN], sz[MAXN];
int getf(int k) { return k == f[k] ? k : f[k] = getf(f[k]); }
bool loop[MAXN], cycle[MAXN];
int main() {
  int n, x, y, u, v;
  cin >> n;
  for (int i = 0; i <= 2 * n; i++) f[i] = i, sz[i] = 1;
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &x, &y);
    if (x == y) loop[getf(x)] = 1;
    u = getf(x), v = getf(y);
    if (u != v) {
      f[u] = v;
      sz[v] += sz[u];
      loop[v] |= loop[u];
    } else
      cycle[u] = 1;
  }
  long long ans = 1;
  for (int i = 1; i <= 2 * n; i++) {
    if (getf(i) == i) {
      if (loop[i]) continue;
      if (cycle[i])
        ans = ans * 2 % mod;
      else
        ans = ans * sz[i] % mod;
    }
  }
  cout << ans << endl;
  return 0;
}
