#include <bits/stdc++.h>
using namespace std;
bool ind = 0;
const int N = 2e5 + 100, M = 1e9 + 7;
int64_t n, m, ans = 1, vis[N], x, y, s[N], t, nex[N], ans1, val[N], sig;
vector<int64_t> adjt[N], adj[N];
int64_t dfs(int64_t node) {
  int64_t cnt = 1;
  vis[node] = 2;
  for (int64_t i : adjt[node]) cnt += dfs(i);
  return cnt;
}
void dfsc(int64_t node, int64_t put = 0) {
  vis[node]++;
  val[node] = put;
  for (int64_t i : adj[node])
    if (vis[i] == 1) {
      sig = 1;
      ans1 = val[node] - val[i] + 1;
    } else if (vis[i] == 0)
      dfsc(i, put + 1);
  vis[node]++;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int64_t i = 1; i <= n; i++) {
    cin >> x >> y;
    adjt[y].push_back(x);
    adj[x].push_back(y);
    nex[x] = y;
    s[x] = 1;
  }
  for (int64_t i = 1; i <= n << 1; i++) {
    if (s[i] == 0 && adjt[i].size()) ans = (ans * dfs(i)) % M;
  }
  for (int64_t i = 1; i <= n << 1; i++) {
    if (s[i] == 1 && vis[i] == 0) {
      sig = 0;
      ans1 = 0;
      dfsc(i);
      if (sig && ans1 > 1) ans = (ans << 1) % M;
    }
  }
  cout << ans << '\n';
}
