#include #include namespace my{ void main(); void solve(); } int main(){my::main();} namespace my{ #define eb emplace_back #define LL(...) ll __VA_ARGS__;lin(__VA_ARGS__) #define VV(n,...) vec__VA_ARGS__;setsize({n},__VA_ARGS__);vin(__VA_ARGS__) #define FO(n) for(ll ij=0;ij>(istream&i,ulll&x){ull t;i>>t;x=t;return i;} ostream&operator<<(ostream&o,const ulll&x){return(x<10?o:o<>(istream&i,lll&x){ll t;i>>t;x=t;return i;} ostream&operator<<(ostream&o,const lll&x){return o<0?x:-x);} constexpr char nl=10; constexpr char sp=32; auto range(bool s,ll a,ll b=9e18,ll c=1){if(b==9e18)b=a,(s?b:a)=0;return tuple{a-s,b,c};} ll rand(ll l=9e18,ll r=9e18){static ull a=495;a^=a<<7,a^=a>>9;return r!=9e18?a%(r-l)+l:l!=9e18?a%l:a;} lll pw(lll x,lll n,ll m=0){assert(n>=0);lll r=1;while(n)n&1?r*=x:r,x*=x,m?r%=m,x%=m:r,n>>=1;return r;} templateauto max(const A&...a){return max(initializer_list>{a...});} templatestruct pair{ A a;B b; pair()=default; pair(A a,B b):a(a),b(b){} pair(std::pairp):a(p.first),b(p.second){} bool operator==(const pair&p)const{return a==p.a&&b==p.b;} auto operator<=>(const pair&p)const{return a!=p.a?a<=>p.a:b<=>p.b;} friend ostream&operator<<(ostream&o,const pair&p){return o<>auto&sort(auto&a,F f={}){ranges::sort(a,f);return a;} templateistream&operator>>(istream&i,pair&p){return i>>p.first>>p.second;} templateostream&operator<<(ostream&o,const pair&p){return o<ostream&operator<<(ostream&o,const unordered_map&m){fe(m,e)o<ostream&operator<<(ostream&o,const bitset&b){fo(i,n)o<concept isv=is_base_of_v,V>; templatestruct core_type{using type=T;}; templatestruct core_type{using type=typename core_type::type;}; templateistream&operator>>(istream&i,vector&v){fe(v,e)i>>e;return i;} templateostream&operator<<(ostream&o,const vector&v){fe(v,e)o<?nl:sp);return o;} templatestruct vec:vector{ using vector::vector; vec(const vector&v){fe(v,e)this->eb(e);} templatevec(const bitset&a){fo(i,n)this->eb(a[i]);} vec&operator+=(const vec&u){vec&v=*this;fo(i,v.size())v[i]+=u[i];return v;} vec&operator-=(const vec&u){vec&v=*this;fo(i,v.size())v[i]-=u[i];return v;} vec&operator^=(const vec&u){fe(u,e)this->eb(e);return*this;} vec operator+(const vec&u)const{return vec(*this)+=u;} vec operator-(const vec&u)const{return vec(*this)-=u;} vec operator^(const vec&u)const{return vec(*this)^=u;} vec&operator++(){fe(*this,e)++e;return*this;} vec&operator--(){fe(*this,e)--e;return*this;} vec operator-()const{vec v=*this;fe(v,e)e=-e;return v;} auto scan(auto f)const{pair::type,bool>r{};fe(*this,e)if constexpr(!isv)r.b?f(r.a,e),r:r={e,1};else if(auto s=e.scan(f);s.b)r.b?f(r.a,s.a),r:r=s;return r;} auto max()const{return scan([](auto&a,const auto&b){aauto make_vec(const ll(&s)[n],T x={}){if constexpr(n==i+1)return vec(s[i],x);else{auto X=make_vec(s,x);return vec(s[i],X);}} templatevoid setsize(const ll(&l)[n],A&...a){((a= make_vec(l,typename core_type::type())),...);} void io(){cin.tie(0)->sync_with_stdio(0);cout<>...>>a);} void vin(auto&...a){fo(i,(a.size()&...))(cin>>...>>a[i]);} templatevoid pp(const auto&...a){ll n=sizeof...(a);((cout<0,c)),...);cout<auto rle(const vec&a){vec>r;fe(a,e)r.size()&&e==r.back().a?++r.back().b:r.eb(e,1).b;return r;} templateauto rce(veca){return rle(sort(a));} namespace sgt{ templateT max(T a,T b){return b>64; return r>=m?r-m:r; } auto&operator+=(const montgomery64&b){if((a+=b.a)>=m)a-=m;return*this;} auto&operator-=(const montgomery64&b){if(i64(a-=b.a)<0)a+=m;return*this;} auto&operator*=(const montgomery64&b){a=reduce(u128(a)*b.a);return*this;} auto&operator/=(const montgomery64&b){*this*=b.inv();return*this;} auto operator+(const montgomery64&b)const{return montgomery64(*this)+=b;} auto operator-(const montgomery64&b)const{return montgomery64(*this)-=b;} auto operator*(const montgomery64&b)const{return montgomery64(*this)*=b;} auto operator/(const montgomery64&b)const{return montgomery64(*this)/=b;} bool operator==(const montgomery64&b)const{return a==b.a;} bool operator!=(const montgomery64&b)const{return a!=b.a;} auto operator-()const{return montgomery64()-montgomery64(*this);} montgomery64 pow(u128 n)const{ montgomery64 r(1),x(*this); while(n){ if(n&1)r*=x; x*=x; n>>=1; } return r; } montgomery64 inv()const{ u64 a=this->a,b=m,u=1,v=0; while(b)u-=a/b*v,swap(u,v),a-=a/b*b,swap(a,b); return u; } u64 val()const{ return reduce(a); } friend istream&operator>>(istream&i,montgomery64&b){ ll t;i>>t;b=t; return i; } friend ostream&operator<<(ostream&o,const montgomery64&b){ return o<bool miller_rabin(ll n,vecas){ ll d=n-1; while(~d&1)d>>=1; if(modint::mod()!=n)modint::set_mod(n); modint one=1,minus_one=n-1; fe(as,a){ if(a%n==0)continue; ll t=d; modint y=modint(a).pow(t); while(t!=n-1&&y!=one&&y!=minus_one)y*=y,t<<=1; if(y!=minus_one&&~t&1)return 0; } return 1; } bool is_prime(ll n){ if(~n&1)return n==2; if(n<=1)return 0; if(n<4759123141LL)return miller_rabin(n,{2,7,61}); return miller_rabin(n,{2,325,9375,28178,450775,9780504,1795265022}); } templatell pollard_rho(ll n){ if(~n&1)return 2; if(is_prime(n))return n; if(modint::mod()!=n)modint::set_mod(n); modint R,one=1; auto f=[&](const modint&x){return x*x+R;}; while(1){ modint x,y,ys,q=one; R=rand(2,n),y=rand(2,n); ll g=1; constexpr ll m=128; for(ll r=1;g==1;r<<=1){ x=y; fo(r)y=f(y); for(ll k=0;g==1&&kinner_factorize(ll n){ if(n<=1)return{}; ll d=pollard_rho(n); if(d==n)return{d}; return inner_factorize(d)^inner_factorize(n/d); } vec>factorize(ll n){ return rce(inner_factorize(n)); } templateT mod(T a,T m){a%=m;return a<0?a+m:a;} templateT gcd(T a,T b){return b?gcd(b,a%b):a;} templateauto gcd(const A&...a){common_type_tr=0;((r=gcd(r,a)),...);return r;} templateT lcm_mod(const vec&a,T M){ unordered_mapmem; fo(i,a.size())fe(factorize(a[i]),p,b)if(!mem.count(p)||mem[p]pairax_by_g(T a,T b){ if(b==0)return{1,0}; auto[s,t]=ax_by_g(b,a%b); return{t,s-a/b*t}; } ll inv_mod(ll a,ll m){ assert(gcd(a,m)==1); auto[x,y]=ax_by_g(a,m); return mod(x,m); } templateT chinese_remainder_theorem_coprime(const vec&a,const vec&m,T M=0){ ll K=a.size(); vect(K),S(K),P(K,1); T SM=0,PM=1; fo(i,K){ t[i]=mod((a[i]-S[i])*inv_mod(P[i],m[i]),m[i]); fo(j,i+1,K){ (S[j]+=t[i]*P[j]%m[j])%=m[j]; (P[j]*=m[i])%=m[j]; } if(M){ (SM+=t[i]*PM%M)%=M; (PM*=m[i])%=M; }else{ SM+=t[i]*PM; PM*=m[i]; } } return SM; } templateT chinese_remainder_theorem(const vec&a,const vec&m,T M=0){ ll K=a.size(); fo(i,K)fo(j,i+1,K)if((a[i]-a[j])%gcd(m[i],m[j]))return-1; unordered_map>mem; fo(i,K)fe(factorize(m[i]),p,b)if(!mem.count(p)||mem[p].bpa,pm; fe(mem,p,v){ T mod=pw(p,v.b); pa.eb(v.a%mod); pm.eb(mod); } return chinese_remainder_theorem_coprime(pa,pm,M); } void main(){io();ll T=1;fo(T)solve();} void solve(){ LL(N); VV(N,a,m); ll M=atcoder::modint1000000007::mod(); pp(a.max()==0?lcm_mod(m,M):chinese_remainder_theorem(a,m,M)); }}