#include <bits/stdc++.h>
using namespace std;
long long INF = LONG_LONG_MAX / 2;
int inf = INT_MAX / 2;
long long mod = 1000000000 + 7;
template <typename T, typename L>
void read(vector<T>& _data, L _size, int _shift) {
  _data.resize(_size + _shift);
  for (long long i = _shift; i < _size + _shift; i++) cin >> _data[i];
}
template <typename T, typename L>
void read(vector<vector<T>>& _data, L _rows, L _cols, int _shiftRows,
          int _shiftCols) {
  _data.resize(_rows + _shiftRows);
  for (long long i = 0; i < _rows + _shiftRows; i++)
    _data[i].resize(_cols + _shiftCols);
  for (long long i = _shiftRows; i < _rows + _shiftRows; i++)
    for (long long j = _shiftCols; j < _cols + _shiftCols; j++)
      cin >> _data[i][j];
}
template <typename T>
void write(vector<T>& _data, long long _shift) {
  for (long long i = _shift; i < _data.size(); i++) cout << _data[i] << " ";
  cout << endl;
}
int n;
const int N = (int)2e5 + 5;
long long ans = 1;
vector<int> roots;
vector<int> graph[N];
bool been[N];
int mark[N];
int pos[N];
int want[N];
int seater[N];
void dfsmark(int x, int p) {
  been[x] = true;
  if (seater[x]) {
    int i = seater[x];
    graph[want[i]].push_back(x);
    if (!been[want[i]])
      dfsmark(want[i], x);
    else if (!mark[want[i]]) {
      mark[x] = 2;
      if (want[i] != x) ans = (ans * 2) % mod;
      return;
    }
    mark[x] = mark[want[i]];
    return;
  }
  roots.push_back(x);
  mark[x] = 1;
}
long long dfsroot(int x) {
  long long score = 1;
  for (int y : graph[x]) {
    score = (score + dfsroot(y)) % mod;
  }
  return score;
}
void solve() {
  cin >> n;
  for (int i = (1); i <= int(n); i++) {
    cin >> pos[i];
    cin >> want[i];
    seater[pos[i]] = i;
  }
  for (int i = (1); i <= int(n); i++) {
    if (!been[pos[i]]) dfsmark(pos[i], 0);
  }
  for (int x : roots) {
    ans = (ans * dfsroot(x)) % mod;
  }
  cout << ans << endl;
}
int main() {
  ios::sync_with_stdio(false);
  bool _multipleTestCases = false;
  if (_multipleTestCases) {
    long long t;
    cin >> t;
    while (t--) solve();
  } else {
    solve();
  }
  return 0;
}
