#include <bits/stdc++.h>
using namespace std;
int const MAXN = 2e6 + 9;
int const MOD = 1e9 + 7;
int vis[MAXN];
long long sz;
vector<vector<int>> adj;
long long mul(long long a, long long b) { return (a * b) % MOD; }
bool cond;
bool loop;
void DFS(int node, int p) {
  vis[node] = true;
  sz++;
  for (auto child : adj[node]) {
    if (child == node) {
      loop = true;
    }
    if (!vis[child]) {
      DFS(child, node);
    } else if (child != p) {
      cond = true;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0);
  int n;
  cin >> n;
  adj.resize(2 * n + 2);
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  long long ans = 1;
  for (int i = 1; i <= 2 * n; i++) {
    if (!vis[i]) {
      sz = 0;
      cond = false;
      loop = false;
      DFS(i, i);
      if (cond) sz = 2;
      if (loop) sz = 1;
      ans = mul(ans, sz);
    }
  }
  cout << ans;
}
