#include <bits/stdc++.h>
using namespace std;
const int MAXN = (1 << 20);
const int64_t mod = (int64_t)1e9 + 7;
int n;
vector<int> G[MAXN];
int a[MAXN], to[MAXN];
bool has[MAXN];
void read() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int u, v;
    cin >> u >> v;
    G[u].push_back(v);
    has[u] = 1;
  }
  n *= 2;
}
stack<int> st;
vector<vector<int>> scc;
int low[MAXN], disc[MAXN], comp[MAXN];
int dfs_time;
bool in_stack[MAXN];
void dfs(int u) {
  low[u] = dfs_time;
  disc[u] = dfs_time;
  dfs_time++;
  in_stack[u] = true;
  st.push(u);
  int sz = G[u].size(), v;
  for (int i = 0; i < sz; i++) {
    v = G[u][i];
    if (disc[v] == -1) {
      dfs(v);
      low[u] = min(low[u], low[v]);
    } else if (in_stack[v] == true)
      low[u] = min(low[u], disc[v]);
  }
  if (low[u] == disc[u]) {
    scc.push_back(vector<int>());
    while (st.top() != u) {
      scc[scc.size() - 1].push_back(st.top());
      in_stack[st.top()] = false;
      st.pop();
    }
    scc[scc.size() - 1].push_back(u);
    in_stack[u] = false;
    st.pop();
  }
}
set<int> adj[MAXN];
int comp_sz[MAXN];
int in_deg[MAXN];
void tarjan() {
  memset(comp_sz, 0, sizeof(comp_sz));
  memset(comp, -1, sizeof(comp));
  memset(disc, -1, sizeof(disc));
  memset(low, -1, sizeof(low));
  memset(in_stack, 0, sizeof(in_stack));
  dfs_time = 0;
  while (!st.empty()) st.pop();
  for (int i = 1; i <= n; i++)
    if (disc[i] == -1) dfs(i);
  int sz = scc.size();
  for (int i = 0; i < sz; i++)
    for (int j = 0; j < (int)scc[i].size(); j++)
      comp_sz[i]++, comp[scc[i][j]] = i;
  for (int i = 1; i <= n; i++)
    for (int v : G[i])
      if (comp[v] != comp[i] &&
          adj[comp[v]].find(comp[i]) == adj[comp[v]].end())
        adj[comp[v]].insert(comp[i]), in_deg[comp[i]]++;
}
int64_t pow(int64_t x, int64_t p, int64_t MOD) {
  if (p == 0) return 1;
  if (p % 2 == 1) return (x * pow(x, p - 1, MOD)) % MOD;
  int64_t half = pow(x, p / 2, MOD);
  return (half * half) % MOD;
}
int64_t rec(int u) {
  int64_t ret = 1;
  for (int v : adj[u]) ret += rec(v);
  return ret;
}
void solve() {
  tarjan();
  int64_t answer = 1;
  for (int u = 0; u < (int)scc.size(); u++)
    if (in_deg[u] == 0) {
      if (comp_sz[u] == 1) {
        if (!has[scc[u][0]])
          answer = (answer * 1ll * rec(u)) % mod;
        else
          continue;
      } else
        answer = (answer * 2ll) % mod;
    }
  cout << answer << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  read();
  solve();
  return 0;
}
