#include <bits/stdc++.h>
using namespace std;
bool vis_parent[200001];
bool vis_compute[200001];
bool vis_loop[200001];
bool vis_self_loop[200001];
int move_to[200001];
vector<int> move_from[200001];
int parents[200001];
int N;
void fill_vis_compute(int i) {
  vis_compute[i] = true;
  for (int j : move_from[i]) {
    if (!vis_compute[j]) {
      fill_vis_compute(j);
    }
  }
  if (!vis_compute[move_to[i]]) {
    fill_vis_compute(move_to[i]);
  }
}
bool find_loop(int i) {
  if (vis_parent[i]) {
    return true;
  }
  vis_parent[i] = true;
  if (move_to[i] > 0) {
    return find_loop(move_to[i]);
  } else {
    return false;
  }
}
bool find_self_loop(int i) {
  if (move_to[i] == i) {
    return true;
  }
  vis_self_loop[i] = true;
  if (move_to[i] > 0 && !vis_self_loop[move_to[i]]) {
    return find_self_loop(move_to[i]);
  }
  return false;
}
int find_root(int vtx) {
  if (move_to[vtx] > 0) {
    return find_root(move_to[vtx]);
  } else {
    return vtx;
  }
}
int compute_answer(int vtx) {
  if (vis_compute[vtx]) {
    return 1;
  }
  int ret = 1;
  for (int j : move_from[vtx]) {
    ret += compute_answer(j);
  }
  return ret;
}
int main(int argc, const char* argv[]) {
  cin >> N;
  for (int i = 0; i < N; i++) {
    int s, e;
    cin >> s >> e;
    move_to[s] = e;
    move_from[e].push_back(s);
  }
  long long ans = 1;
  long long mod = 1000000007;
  for (int i = 1; i <= 2 * N; i++) {
    if (find_self_loop(i)) {
      if (!vis_compute[i]) {
        fill_vis_compute(i);
      }
    } else if (find_loop(i)) {
      if (move_to[i] > 0 && !vis_compute[i]) {
        ans = (ans * 2) % mod;
        fill_vis_compute(i);
      }
    } else {
      ans = (ans * compute_answer(find_root(i))) % mod;
      fill_vis_compute(i);
    }
  }
  cout << ans << endl;
  return 0;
}
