#include <bits/stdc++.h>
using namespace std;
long long int m = 1e9 + 7, par[200005], sz[200005], c[200005], in[200005],
              b[200005];
int f(int i) {
  if (par[i] != i) par[i] = f(par[i]);
  return par[i];
}
void merge(int a, int b) {
  a = f(a), b = f(b);
  if (a == b) return;
  par[b] = a;
  sz[a] += sz[b];
}
vector<int> v[200005];
int main() {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 1; i <= 2 * n; i++) par[i] = i, sz[i] = 1;
  for (int i = 1; i <= n; i++) {
    int x, y;
    cin >> x >> y;
    if (x != y) {
      v[x].push_back(y);
      in[y]++;
      merge(x, y);
    } else {
      b[x] = 1;
    }
  }
  queue<int> q;
  for (int i = 1; i <= 2 * n; i++) {
    if (in[i] == 0) q.push(i);
  }
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    if (in[x] != 0) continue;
    for (__typeof(v[x].begin()) it = v[x].begin(); it != v[x].end(); it++) {
      in[*it]--;
      if (in[*it] == 0) q.push(*it);
    }
  }
  for (int i = 1; i <= 2 * n; i++) {
    if (in[i] != 0) {
      sz[f(i)]--;
      c[f(i)] = 1;
    }
  }
  for (int i = 1; i <= 2 * n; i++) {
    if (b[i]) {
      sz[f(i)] = 1;
    }
  }
  long long int ans = 1;
  for (int i = 1; i <= 2 * n; i++) {
    if (i == f(i)) {
      if (b[i])
        ans = ans;
      else if (c[i])
        ans = (ans * (2)) % m;
      else
        ans = (ans * sz[i]) % m;
    }
  }
  cout << ans;
  return 0;
}
