#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 5;
const long long int mod = 1e9 + 7;
int n;
vector<int> to[maxn];
vector<int> alls[maxn];
vector<bool> used;
vector<int> comp;
vector<bool> is_in;
void dfs(int v) {
  if (used[v]) return;
  used[v] = true;
  comp.push_back(v);
  for (auto it : alls[v]) dfs(it);
}
bool calc_cycle() {
  int v = comp[0];
  for (auto it : comp) {
    if (to[it].size() == alls[it].size()) v = it;
  }
  set<int> st;
  st.insert(v);
  int cur = v;
  bool have = false;
  vector<int> lold;
  lold.push_back(cur);
  while (true) {
    if (to[cur].size() == 0) break;
    cur = to[cur][0];
    if (st.count(cur)) {
      have = true;
      int kek = lold.size() - 1;
      while (true) {
        is_in[lold[kek]] = true;
        if (lold[kek] == cur) break;
        kek--;
      }
      break;
    } else {
      st.insert(cur);
      lold.push_back(cur);
    }
  }
  return have;
}
int main() {
  cin >> n;
  int a, b;
  set<int> st;
  set<int> realm;
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    a--;
    b--;
    to[a].push_back(b);
    alls[a].push_back(b);
    alls[b].push_back(a);
    st.insert(a);
    st.insert(b);
    realm.insert(a);
  }
  used.resize(2 * n);
  is_in.resize(2 * n);
  long long int cur = 1;
  for (auto it : realm) {
    if (used[it]) continue;
    comp.clear();
    dfs(it);
    long long int kek = 0;
    bool is = calc_cycle();
    if (!is) {
      for (auto c : comp) kek++;
    } else {
      for (auto c : comp) {
        if (is_in[c]) kek++;
      }
      if (kek > 2) kek = 2;
    }
    kek = max(kek, 1LL);
    cur *= kek;
    cur %= mod;
  }
  cout << cur << endl;
}
