#include #include #include #include #include #include #include #include #include #include #include template struct Montgomery { static_assert(sizeof(U0) * 2 == sizeof(U1)); constexpr static unsigned B0 = sizeof(U0) * 8U; U0 n, nr, rs, np; constexpr Montgomery(const U0 &Mod) { SetMod(Mod); } constexpr U0 GetMod() const noexcept { return n; } constexpr void SetMod(const U0 &Mod) { assert(Mod >= 2); assert(Mod % 2 == 1); assert(Mod >> (B0 - 2) == 0); n = nr = Mod; rs = -static_cast(n) % n; for (uint32_t i = 0; i < std::bit_width(B0) - 1; i++) { nr *= 2 - n * nr; } np = reduce(static_cast(1), rs); } constexpr U0 reduce(const U0 &x) const noexcept { const U0 q = x * nr; const U0 m = (static_cast(q) * n) >> B0; const U0 res = n - m; return (res >= n) ? res - n : res; } constexpr U0 reduce(const U0 &x, const U0 &y) const noexcept { const U1 t = static_cast(x) * y; const U0 c = t, d = t >> B0; const U0 q = c * nr; const U0 m = (static_cast(q) * n) >> B0; const U0 res = d + n - m; return (res >= n) ? res - n : res; } constexpr U0 reduce(const U0 &x, const U0 &y, const U0 &z) const noexcept { const U1 t = static_cast(x) * y; const U0 c = t, d = t >> B0; const U0 q = c * nr; const U0 m = (static_cast(q) * n) >> B0; U0 res = d + n - m; if (res >= n) { res -= n; } res += z; if (res >= n) { res -= n; } return res; } constexpr U0 val(const U0 &x) const noexcept { return reduce(x); } constexpr U0 zero() const noexcept { return static_cast(0); } constexpr U0 one() const noexcept { return np; } constexpr U0 raw(const U0 &x) const noexcept { return reduce(x, rs); } template requires std::unsigned_integral constexpr U0 trans(const U &x) const noexcept { if (x < n) { return raw(x); } return reduce(x % n, rs); } template requires std::signed_integral constexpr U0 trans(S x) const noexcept { if (0 <= x && x < static_cast(n)) { return raw(x); } x %= static_cast(n); if (x < 0) { x += static_cast(n); } return reduce(x, rs); } constexpr U0 neg(const U0 &x) const noexcept { return (x == 0) ? 0 : (n - x); } constexpr U0 inc(const U0 &x) const noexcept { return add(x, np); } constexpr U0 dec(const U0 &x) const noexcept { return sub(x, np); } constexpr U0 add(const U0 &x, const U0 &y) const noexcept { const U0 z = x + y; return (z >= n) ? z - n : z; } constexpr U0 sub(const U0 &x, const U0 &y) const noexcept { return (x < y) ? (x - y + n) : (x - y); } constexpr U0 mul(const U0 &x, const U0 &y) const noexcept { return reduce(x, y); } constexpr U0 mul_add(const U0 &x, const U0 &y, const U0 &z) const noexcept { return reduce(x, y, z); } constexpr U0 pow(const U0 &x, U0 k) const noexcept { U0 res = np; U0 b = x; while (k > 0) { if (k & 1) { res = mul(res, b); } b = mul(b, b); k >>= 1; } return res; } constexpr bool same(const U0 &x, const U0 &y) const noexcept { return x == y; } }; bool is_prime(uint64_t n) { using u64 = uint64_t; using u128 = __uint128_t; if (n < 2) { return false; } for (u64 p : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37}) { if (n % p == 0) { return n == p; } } if (n < 41 * 41) { return true; } Montgomery mont(n); const u64 one = mont.one(), neg_one = mont.neg(one); auto miller_rabin = [&](const std::vector &bases) -> bool { u64 d = n - 1; int e = 0; while (~d & 1) { d >>= 1; e++; } for (u64 b : bases) { u64 x = mont.pow(mont.trans(b), d); if (x == one) { continue; } for (int i = 0; i < e; i++) { u64 y = mont.mul(x, x); if (y == one) { if (x == neg_one) { break; } return false; } x = y; if (i == e - 1) { return false; } } } return true; }; if (n < 2047) { return miller_rabin({2}); } if (n < 9080191) { return miller_rabin({31, 73}); } if (n < 4759123141) { return miller_rabin({2, 7, 61}); } if (n < 1122004669633) { return miller_rabin({2, 13, 23, 1662803}); } if (n < 3770579582154547) { return miller_rabin({2, 880937, 2570940, 610386380, 4130785767}); } return miller_rabin({2, 325, 9375, 28178, 450775, 9780504, 1795265022}); } std::vector> factorize(uint64_t n) { using namespace std; using u32 = uint32_t; using u64 = uint64_t; using u128 = __uint128_t; vector> res; for (u64 p : {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}) { u32 cnt = 0; while (n % p == 0) { cnt++; n /= p; } if (cnt > 0) { res.emplace_back(p, cnt); } } if (n == 1) { return res; } if (is_prime(n)) { res.emplace_back(n, 1); return res; } auto pollard_rho = [&](u64 n) -> u64 { if (n % 2 == 0) { return 2; } Montgomery mont(n); const u64 one = mont.one(), two = mont.trans(2); constexpr u64 m = 1 << 8; u64 c = one; while (true) { auto f = [&](u64 x) { return mont.mul_add(x, x, c); }; u64 x = two, y = two; while (true) { u64 q = one; vector diffs; diffs.reserve(m); for (u64 i = 0; i < m; i++) { y = f(f(y)); x = f(x); const u64 diff = mont.sub(x, y); diffs.push_back(diff); q = mont.mul(q, diff); } const u64 g = gcd(q, n); if (g == 1) { continue; } if (g != n) { return g; } for (u64 diff : diffs) { const u64 h = gcd(diff, n); if (h != 1 && h != n) { return h; } } c = mont.add(c, one); break; } } }; auto update = [&](u64 p) -> void { for (auto &[q, cnt] : res) { if (p == q) { cnt++; return; } } res.emplace_back(p, 1); }; auto dfs = [&](auto dfs, u64 x) -> void { if (x == 1) { return; } if (is_prime(x)) { update(x); return; } const u64 d = pollard_rho(x); dfs(dfs, d); dfs(dfs, x / d); }; dfs(dfs, n); sort(res.begin(), res.end()); return res; } uint64_t fibonacci(uint64_t n, uint64_t p) { assert(p > 1); using mint = atcoder::modint; mint::set_mod(p); using Mat = std::array, 2>; auto mat_mul = [&](const Mat x, const Mat y) -> Mat { Mat res{}; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { for (int k = 0; k < 2; k++) { res[i][j] += x[i][k] * y[k][j]; } } } return res; }; auto mat_pow = [&](Mat x, uint64_t k) -> Mat { Mat res{{{1, 0}, {0, 1}}}; while (k > 0) { if (k & 1) { res = mat_mul(res, x); } x = mat_mul(x, x); k /= 2; } return res; }; Mat x{{{0, 1}, {1, 1}}}; Mat res = mat_pow(x, n); return res[0][1].val(); } uint64_t pisano_p(uint64_t p) { if (p == 2) { return 3; } if (p == 5) { return 20; } uint64_t res = (p % 5 == 1 || p % 5 == 4) ? p - 1 : p * 2 + 2; auto factors = factorize(res); for (auto [q, e] : factors) { while (res % q == 0) { uint64_t nres = res / q; if (fibonacci(nres, p) == 0 && fibonacci(nres + 1, p) == 1) { res = nres; } else { break; } } } return res; } std::unordered_map pisano_pow_p(uint64_t p, uint32_t k) { std::unordered_map res; res[p] = k - 1; for (auto [q, e] : factorize(pisano_p(p))) { res[q] += e; } return res; } uint64_t pisano() { std::unordered_map mp; uint32_t n; std::cin >> n; while (n--) { uint64_t p; uint32_t k; std::cin >> p >> k; for (auto [q, e] : pisano_pow_p(p, k)) { mp[q] = std::max(mp[q], e); } } using mint = atcoder::modint1000000007; mint res = 1; for (auto [q, e] : mp) { res *= mint(q).pow(e); } return res.val(); } int main() { using namespace std; ios::sync_with_stdio(false); cin.tie(nullptr); cout << pisano() << '\n'; }