#include #include using i64=long long; using u64=unsigned long long; using namespace std; template using modint=atcoder::static_modint; struct primefact{ int n; vector lpf; primefact(int n_):n(n_),lpf(n_+1,-1){ for(i64 p=2;p<=n;++p){ if(lpf[p]!=-1){ continue; } lpf[p]=p; for(i64 q=p;q<=n;q+=p){ if(lpf[q]==-1){ lpf[q]=p; } } } } vector> operator()(int m){ vector v; while(m!=1){ v.emplace_back(lpf[m]); m/=lpf[m]; } if(v.size()==0){ return {}; } vector> res; res.emplace_back(v[0],1); for(size_t i=1;i mint; primefact pf(300'001); int N; cin>>N; vector A(N),B(N); vector>> idx(300'001); vector m(300'001); mint sm=0; for(int i=0;i>A[i]>>B[i]; sm+=mint(A[i])/B[i]; auto tmp=pf(B[i]); for(auto [p,e]:tmp){ idx[p].emplace_back(i,e); m[p]=max(m[p],e); } } mint d=1; for(i64 p=2;p<=300'001;++p){ if(m[p]==0){ continue; } vector pw(21,1); for(int i=1;i<=20;++i){ pw[i]=pw[i-1]*p; } i64 sm=0; for(auto [i,e]:idx[p]){ sm+=A[i]*pw[m[p]-e]*atcoder::inv_mod(B[i]/pw[e],pw[m[p]]); sm%=pw[m[p]]; } i64 prd=pw[m[p]]; for(int i=0;i