#include <bits/stdc++.h>
using namespace std;
vector<int> v[200000 + 1];
vector<int> lead[200000 + 1];
int cycle[200000 + 1];
int vis[200000 + 1];
int is[200000 + 1];
int dp[200000 + 1];
int t[200000 + 1];
int N, cic;
void DFS(int k) {
  int i;
  vis[k] = 1;
  if (!is[t[k]]) lead[t[k]].push_back(k);
  for (i = 0; i < v[k].size(); ++i) {
    if (!vis[v[k][i]]) DFS(v[k][i]);
    dp[k] = (dp[k] + dp[v[k][i]]) % 1000000007;
  }
  ++dp[k];
}
void Cycle_detect() {
  int i, j, f, stp;
  for (i = 1; i <= 2 * N; ++i)
    if (is[i] && cycle[i] == 0) {
      stp = 0;
      for (j = i; !cycle[t[j]] && is[t[j]]; j = t[j]) cycle[j] = -1, ++stp;
      if (cycle[t[j]] > 0) {
        f = cycle[t[j]];
        for (j = i; stp + 1; j = t[j], --stp) cycle[j] = f, vis[j] = 1;
      } else if (cycle[t[j]] == -1) {
        f = t[j];
        ++cic;
        for (j = i; stp + 1; j = t[j], --stp) cycle[j] = cic, vis[j] = 1;
      } else {
        for (j = i; stp + 1; j = t[j], --stp) cycle[j] = -2;
      }
    }
}
int main() {
  int i, j, first, second, res = 1, two = 1;
  scanf("%d", &N);
  for (i = 1; i <= N; ++i) {
    scanf("%d %d", &first, &second);
    v[second].push_back(first);
    is[first] = 1;
    t[first] = second;
  }
  Cycle_detect();
  for (i = 1; i <= 2 * N; ++i)
    if (!vis[i] && is[i]) DFS(i);
  for (i = 1; i <= 2 * N; ++i)
    if (lead[i].size()) {
      for (j = 0; j < lead[i].size(); ++j)
        dp[i] = (dp[i] + dp[lead[i][j]]) % 1000000007;
      ++dp[i];
      res = (1LL * res * dp[i]) % 1000000007;
    }
  for (i = 1; i <= 2 * N; ++i)
    if (t[i] == i) --cic;
  for (i = 1; i <= cic; ++i) two = (1LL * two * 2) % 1000000007;
  printf("%d\n", (1LL * res * two) % 1000000007);
  return 0;
}
