#include <bits/stdc++.h>
using namespace std;
long long n, x, y, d[300005], p[300005], c[300005], ans = 1;
int par(int x) {
  if (x == p[x]) return x;
  return p[x] = par(p[x]);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= 2 * n; i++) p[i] = i, c[i] = 1;
  for (long long i = 1; i <= n; i++) {
    cin >> x >> y;
    if (x == y)
      d[par(x)] = 2;
    else if (par(x) != par(y)) {
      int xx = par(x), yy = par(y);
      p[yy] = xx;
      c[xx] += c[yy];
      d[xx] = max(d[xx], d[yy]);
    } else
      d[par(x)] = 1;
  }
  for (int i = 1; i <= 2 * n; i++)
    if (par(i) == i) {
      if (d[i] == 1) ans *= 2;
      if (!d[i]) ans *= c[i];
      ans %= 1000000007;
    }
  cout << ans << endl;
  return 0;
}
