#include <bits/stdc++.h>
using namespace std;
int n, x, y;
int outdegree[100005 << 1], dp[100005 << 1][2], color[100005 << 1];
vector<int> g[100005 << 1];
vector<int> v;
void bfs(int node) {
  queue<int> q;
  v.clear();
  q.push(node);
  while (!q.empty()) {
    int cur = q.front();
    q.pop();
    v.push_back(cur);
    for (int i = 0; i < g[cur].size(); i++) {
      q.push(g[cur][i]);
    }
  }
}
int cycle;
void dfs(int node) {
  if (cycle) return;
  color[node] = 1;
  for (int i = 0; i < g[node].size(); i++) {
    int to = g[node][i];
    if (!color[to])
      dfs(to);
    else if (color[to] == 1) {
      if (node == to)
        cycle = 1;
      else
        cycle = 2;
      break;
    }
  }
  color[node] = 2;
}
int powmod(int a, int n) {
  if (n == 0) return 1;
  if (n % 2 == 0) return powmod((1ll * a * a) % 1000000007, n / 2);
  return (1ll * a * powmod(a, n - 1)) % 1000000007;
}
int inverse(int x) { return powmod(x, 1000000007 - 2); }
int solve() {
  int res = 1;
  for (int i = 1; i <= n + n; i++) {
    if (outdegree[i] == 0) {
      bfs(i);
      int m = v.size();
      for (int j = m - 1; j >= 0; j--) {
        int to = v[j];
        dp[to][0] = 1;
        for (int k = 0; k < g[to].size(); k++) {
          int from = g[to][k];
          dp[to][0] = (1ll * dp[to][0] * dp[from][0]) % 1000000007;
        }
        dp[to][1] = dp[to][0];
        for (int k = 0; k < g[to].size(); k++) {
          int from = g[to][k];
          int rem =
              (1ll * ((1ll * dp[to][0] * inverse(dp[from][0])) % 1000000007) *
               dp[from][1]) %
              1000000007;
          dp[to][1] = (dp[to][1] + rem) % 1000000007;
        }
      }
      res = (1ll * res * dp[i][1]) % 1000000007;
    } else {
      cycle = 0;
      dfs(i);
      if (cycle == 2) {
        res = (res + res) % 1000000007;
      }
    }
  }
  return res;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &x);
    scanf("%d", &y);
    g[y].push_back(x);
    outdegree[x]++;
  }
  printf("%d\n", solve());
  return 0;
}
