結果

問題 No.2558 中国剰余定理
コンテスト
ユーザー ryoku
提出日時 2026-08-14 12:31:43
言語 C++23(gcc16)
(gcc 16.1.0 + boost 1.90.0)
コンパイル:
g++-16 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 1 ms / 2,000 ms
+ 473µs
コード長 50,741 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 5,781 ms
コンパイル使用メモリ 343,952 KB
実行使用メモリ 9,384 KB
最終ジャッジ日時 2026-08-14 12:31:59
合計ジャッジ時間 7,594 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#line 1 "lib/template.hpp"
#ifdef TEMPLATE
#else
#define TEMPLATE
# pragma GCC optimize("O3")
using namespace std;
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <climits>
#include <cassert>
#include <functional>
#include <iterator>
#include <utility>
#include <complex>
#include <bitset>
#include <chrono>
#include <random>
#include <limits>
#include <optional>
#include <variant>
#include <any>

#include <array>
#include <bit>
#include <compare>
#include <concepts>
#include <numbers>
#include <ranges>
#include <span>
#include <string_view>
#include <tuple>
#include <type_traits>
#include <version>
using uint=unsigned;
using ll=long long;
using ull=unsigned long long;
using ld=long double;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
using i128=__int128;
using u128=unsigned __int128;
template<class T>using vc=vector<T>;
template<class T>using vvc=vc<vc<T>>;
template<class T>using vvvc=vvc<vc<T>>;
template<class T>using smpq=priority_queue<T,vector<T>,greater<T>>;
template<class T>using bipq=priority_queue<T>;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define REP(i,j,n) for(ll i=(j);i<(ll)(n);i++)
#define DREP(i,n,m) for(ll i=(n);i>=(m);i--)
#define drep(i,n) for(ll i=((n)-1);i>=0;i--)
#define rall(x) x.rbegin(),x.rend()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define is insert
#define bg begin()
#define ed end()
#define all(x) x.begin(),x.end()
void scan(int&a) { cin >> a; }
void scan(ll&a) { cin >> a; }
void scan(string&a) { cin >> a; }
void scan(char&a) { cin >> a; }
void scan(uint&a) { cin >> a; }
void scan(ull&a) { cin >> a; }
void scan(bool&a) { cin >> a; }
void scan(ld&a){ cin>> a;}
template<class T> void scan(vector<T>&a) { for(auto&x:a) scan(x); }
void read() {}
template<class Head, class... Tail> void read(Head&head, Tail&... tail) { scan(head); read(tail...); }
#define INT(...) int __VA_ARGS__; read(__VA_ARGS__);
#define LL(...) ll __VA_ARGS__; read(__VA_ARGS__);
#define ULL(...) ull __VA_ARGS__; read(__VA_ARGS__);
#define STR(...) string __VA_ARGS__; read(__VA_ARGS__);
#define VC(type, name, ...) vector<type> name(__VA_ARGS__); read(name);
#define VVC(type, name, size, ...) vector<vector<type>> name(size, vector<type>(__VA_ARGS__)); read(name);
template<class T>void print(T a) { cout << a; }
template<class T> void print(vector<T>a) { for(int i=0;i<(int)a.size();i++){if(i)cout<<" ";print(a[i]);}cout<<endl;}
void PRT() { cout <<endl; return ; }
template<class T> void PRT(T a) { print(a); cout <<endl; return; }
template<class Head, class... Tail> void PRT(Head head, Tail ... tail) { print(head); cout << " "; PRT(tail...); return; }
template<class T,class F>
bool chmin(T &x, F y){
    if(x>y){
        x=y;
        return true;
    }
    return false;
}
template<class T, class F>
bool chmax(T &x, F y){
    if(x<y){
        x=y;
        return true;
    }
    return false;
}
template <typename T>
T floor(T a, T b) {
  return a / b - (a % b && (a ^ b) < 0);
}
template <typename T>
T ceil(T x, T y) {
  return floor(x + y - 1, y);
}
template <typename T>
T bmod(T x, T y) {
  return x - y * floor(x, y);
}
template <typename T>
pair<T, T> divmod(T x, T y) {
  T q = floor(x, y);
  return {q, x - q * y};
}
void YesNo(bool b){
    cout<<(b?"Yes":"No")<<endl;
}
void YESNO(bool b){
    cout<<(b?"YES":"NO")<<endl;
}
void AliceBob(bool b){
    cout<<(b?"Alice":"Bob")<<endl;
}
void TakahashiAoki(bool b){
    cout<<(b?"Takahashi":"Aoki")<<endl;
}
void Yes(){
    cout<<"Yes"<<endl;
}
void No(){
    cout<<"No"<<endl;
}
vc<int>stovi(const string&s,const string&S){
    vc<int>v(s.size());
    rep(i,s.size()){
        auto t=S.find(s[i]);
        assert(t!=string::npos);
        v[i]=t;
    }
    return v;
}
template<class T=ll>
T isqrt(T x){
    T F=sqrtl(x);
    while((F+1)*(F+1)<=x)F++;
    while(F*F>x)F--;
    return F;
}
template<class T>
vvc<T>trans(const vvc<T>&a){
    assert(a.size()&&a[0].size());
    vvc<T>b(a[0].size(),vc<T>(a.size()));
    rep(i,a.size())rep(j,a[0].size()){
        b[j][i]=a[i][j];
    }
    return b;
}
template<class T>
vc<string>trans(const vc<string>&a){
    assert(a.size()&&a[0].size());
    vc<string>b(a[0].size(),string(a.size(),0));
    rep(i,a.size())rep(j,a[0].size()){
        b[j][i]=a[i][j];
    }
    return b;
}
template<class T>
int popcount(T n){
    return __builtin_popcountll(n);
}
template<class T,class L=ll>
L sum(vc<T>&a){
    return accumulate(all(a),L(0));
}
template<class T>
vc<T>subset(T S){
    vc<T>ans;
    for(T x=S;x>0;x=(x-1)&S)ans.pb(x);
    ans.pb(0);
    return ans;
}
template<class T>
T max(vc<T>&a){
    return *max_element(all(a));
}
template<class T>
T min(vc<T>&a){
    return *min_element(all(a));
}
#ifndef COMPRESSER_STRUCT
#define COMPRESSER_STRUCT
template<class T>
struct Compresser{
    vc<T>x;
    Compresser(int n=0){x.reserve(n);}
    Compresser(const vc<T>&xs){
        x=xs;
    }
    void push(T p){built=false;x.pb(p);}
    bool built=false;
    void build(){
        if(!chmax(built,1))return;
        sort(all(x));
        x.erase(unique(all(x)),x.end());
    }
    int find(T v){
        build();
        auto itr=lower_bound(all(x),v)-x.begin();
        if(itr==x.size()||x[itr]!=v)return -1;
        return itr;
    }
    int find_next(T v){
        build();
        return lower_bound(all(x),v)-x.begin();
    }
    int size(){
        build();
        return x.size();
    }
    T operator[](int i)const{
        assert(0<=i&&i<x.size());
        return x[i];
    }
};
#endif
template<class T,class L=ll>
vc<L> presum(vc<T> &a){
    vc<L> ret(a.size()+1);
    rep(i,a.size())ret[i+1]=ret[i]+a[i];
    return ret;
}
template<class T, class F>
vc<T> &operator+=(vc<T> &a,F b){
    for (auto&v:a)v += b;
    return a;
}
template<class T, class F>
vc<T> &operator-=(vc<T>&a,F b){
    for (auto&v:a)v-=b;
    return a;
}
template<class T, class F>
vc<T> &operator*=(vc<T>&a,F b){
    for (auto&v:a)v*=b;
    return a;
}
template<class T=ll>
constexpr T POW(T a,T b){
    T res=1;
    while(b){
        if(b&1)res*=a;
        a*=a;
        b/=2;
    }
    return res;
}
constexpr ll ten(ll a){
    return POW<ll>(10,a);
}
template<typename T>constexpr T inf=numeric_limits<T>::max()/2-1;
template<class T>
int tbit(T x){
    using U=make_unsigned_t<T>;
    U y=(U)x;
    return y?(int)bit_width(y)-1:-1;
}
template<class T>
int lbit(T x){
    using U=make_unsigned_t<T>;
    U y=(U)x;
    return y?(int)countr_zero(y):-1;
}
template<class T>
int tbit(T x,int p){
    using U=make_unsigned_t<T>;
    constexpr int W=numeric_limits<U>::digits;
    U y=(U)x;
    if(p<0)return -1;
    if(p>=W-1)return tbit(y);
    return tbit(y&((U(1)<<(p+1))-1));
}
template<class T>
int lbit(T x,int p){
    using U=make_unsigned_t<T>;
    constexpr int W=numeric_limits<U>::digits;
    U y=(U)x;
    if(p<0)return lbit(y);
    if(p>=W)return -1;
    return lbit(y&(~U(0)<<p));
}
istream& operator>>(istream&is,i128&x){
    string s;is>>s;
    x=0;
    int i=0,neg=0;
    if(s[0]=='-')neg=1,i=1;
    for(;i<(int)s.size();i++)x=x*10+s[i]-'0';
    if(neg)x=-x;
    return is;
}
ostream& operator<<(ostream&os,i128 x){
    if(x==0)return os<<0;
    if(x<0)os<<"-";
    using u128=__uint128_t;
    u128 y=x<0?-(u128)x:(u128)x;
    string s;
    while(y)s.pb('0'+y%10),y/=10;
    reverse(all(s));
    return os<<s;
}
#define dbg(...) 1111

#ifdef LOCAL
#undef dbg

template<class T,class U>
ostream& operator<<(ostream&os,const pair<T,U>&p){
    return os<<"("<<p.fi<<", "<<p.se<<")";
}
template<class T,size_t N>
ostream& operator<<(ostream&os,const array<T,N>&a){
    os<<"[";
    rep(i,N){
        if(i)os<<", ";
        os<<a[i];
    }
    return os<<"]";
}
template<class T>
ostream& operator<<(ostream&os,const vc<T>&a){
    os<<"[";
    rep(i,a.size()){
        if(i)os<<", ";
        os<<a[i];
    }
    return os<<"]";
}
template<class T>
ostream& operator<<(ostream&os,const deque<T>&a){
    os<<"[";
    rep(i,a.size()){
        if(i)os<<", ";
        os<<a[i];
    }
    return os<<"]";
}
template<class T>
ostream& operator<<(ostream&os,const set<T>&s){
    os<<"{";
    bool f=0;
    for(auto&x:s){
        if(f)os<<", ";
        f=1;
        os<<x;
    }
    return os<<"}";
}
template<class T>
ostream& operator<<(ostream&os,const multiset<T>&s){
    os<<"{";
    bool f=0;
    for(auto&x:s){
        if(f)os<<", ";
        f=1;
        os<<x;
    }
    return os<<"}";
}
template<class T>
ostream& operator<<(ostream&os,const unordered_set<T>&s){
    os<<"{";
    bool f=0;
    for(auto&x:s){
        if(f)os<<", ";
        f=1;
        os<<x;
    }
    return os<<"}";
}
template<class T,class U>
ostream& operator<<(ostream&os,const map<T,U>&m){
    os<<"{";
    bool f=0;
    for(auto&x:m){
        if(f)os<<", ";
        f=1;
        os<<x;
    }
    return os<<"}";
}
template<class T,class U>
ostream& operator<<(ostream&os,const unordered_map<T,U>&m){
    os<<"{";
    bool f=0;
    for(auto&x:m){
        if(f)os<<", ";
        f=1;
        os<<x;
    }
    return os<<"}";
}
template<class T>
ostream& operator<<(ostream&os,queue<T>q){
    vc<T>a;
    while(q.size())a.pb(q.front()),q.pop();
    return os<<a;
}
template<class T>
ostream& operator<<(ostream&os,stack<T>s){
    vc<T>a;
    while(s.size())a.pb(s.top()),s.pop();
    return os<<a;
}
template<class T,class C,class F>
ostream& operator<<(ostream&os,priority_queue<T,C,F>q){
    vc<T>a;
    while(q.size())a.pb(q.top()),q.pop();
    return os<<a;
}

void debug_out(){cout<<endl;}
template<class T,class... Ts>
void debug_out(const T&x,const Ts&...xs){
    cout<<x;
    if constexpr(sizeof...(xs))cout<<" ",debug_out(xs...);
    else cout<<endl;
}
#define dbg(...) cout<<"["<<#__VA_ARGS__<<"] = ",debug_out(__VA_ARGS__)
#endif

struct TemplateSetup{
    TemplateSetup(){
        #ifdef LOCAL
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
        
        #endif
        cin.tie(0)->sync_with_stdio(0);
        #ifdef LOCAL
        cout<<fixed<<setprecision(6);
        dbg("==============="s);
        #else
        cout<<fixed<<setprecision(20);
        #endif
    }
};
inline TemplateSetup template_setup;

#endif
#line 2 "lib/math/barrett.hpp"
struct Barrett{
    using u64=uint64_t;
    using u32=uint32_t;
    using i128=__int128_t;
    u64 m;
    int mod;
    void set(int mod_){
        assert(mod_>0);
        mod=mod_;
        m=(i128(1)<<64)/mod;
    }
    unsigned reduce(uint64_t x){
        assert(mod>0);
        x-=(((i128)x*m)>>64)*mod;
        return x<mod?x:x-mod;
    }
};
#line 3 "lib/math/dynamic-mod-int.hpp"
template<int id>
struct DynamicModInt{
    using u32=uint32_t;
    using u64=uint64_t;
    u32 val;
    DynamicModInt():val(0){}
    DynamicModInt(ll x){
        ll v=x%get_mod();
        if(v<0)v+=get_mod();
        val=v;
    }
    static DynamicModInt raw(int v){
        assert(v>=0);
        DynamicModInt mi;
        mi.val=v;
        return mi;
    }
    DynamicModInt &operator+=(const DynamicModInt&m){
        if((val+=m.val)>=get_mod())val-=get_mod();
        return *this;
    }
    DynamicModInt &operator-=(const DynamicModInt&m){
        if((val+=(get_mod()-m.val))>=get_mod())val-=get_mod();
        return *this;
    }
    DynamicModInt &operator*=(const DynamicModInt&m){
        val=rem(u64(val)*m.val);
        return *this;
    }
    DynamicModInt &operator/=(const DynamicModInt&m){
        val=rem(u64(val)*m.inv().val);
        return *this;
    }
    DynamicModInt operator-() const{
        return DynamicModInt(val?get_mod()-val:0);
    }
    DynamicModInt operator+() const {
        return *this;
    }
    friend DynamicModInt operator+(DynamicModInt lhs, const DynamicModInt& rhs){
        return lhs+=rhs;
    }
    friend DynamicModInt operator-(DynamicModInt lhs, const DynamicModInt& rhs){
        return lhs-=rhs;
    }
    friend DynamicModInt operator*(DynamicModInt lhs, const DynamicModInt& rhs){
        return lhs*=rhs;
    }
    friend DynamicModInt operator/(DynamicModInt lhs,const DynamicModInt&rhs){
        return lhs/=rhs;
    }
    bool operator==(const DynamicModInt&p) const{
        return p.val==val;
    }
    bool operator!=(const DynamicModInt&p) const{
        return p.val!=val;
    }
    DynamicModInt pow(int64_t n) const{
        DynamicModInt res(1),mul(val);
        while(n){
            if(n%2)res*=mul;
            mul*=mul;
            n/=2;
        }
        return res;
    }

    friend ostream&operator<<(ostream&os,const DynamicModInt&p){
        os<<p.val;
        return os;
    }
    friend istream&operator>>(istream&is,DynamicModInt&p){
        int64_t x;
        is>>x;
        p=DynamicModInt(x);
        return is;
    }
    DynamicModInt inv()const{
        int64_t a=val,b=get_mod(),u=1,v=0,t;
        #ifdef LOCAL
        assert(gcd(a,b)==1);
        #endif
        while(b>0){
            t=a/b;
            swap(a-=t*b,b);
            swap(u-=t*v,v);
        }
        return DynamicModInt(u);
    }
    inline static u32 rem(u64 x){return BarrettReduction().reduce(x);}
    static inline int &get_mod(){
        static int mod=0;
        return mod;
    }
    static void set_mod(int md){
        assert(0<md&&md<=(1ll<<31)-1);
        get_mod()=md;
        BarrettReduction().set(md);
    }
    static inline Barrett&BarrettReduction(){
        static Barrett b;
        return b;
    }
};

#line 3 "lib/math/mod.hpp"
template<class,class=void>
struct BinomHasGetMod:false_type{};
template<class mint>
struct BinomHasGetMod<mint,void_t<decltype(mint::get_mod())>>:true_type{};
template<class mint>
struct Binom{
private:
    static vector<mint>&fact_table(){static vector<mint>v={1};return v;}
    static vector<mint>&invfact_table(){static vector<mint>v={1};return v;}
    static vector<mint>&invs_table(){static vector<mint>v={0};return v;}
    static int&built_mod(){static int mod=-1;return mod;}
public:
    static void build(int n){
        auto&_fact=fact_table();
        auto&_invfact=invfact_table();
        auto&_invs=invs_table();
        if constexpr(BinomHasGetMod<mint>::value){
            auto mod=mint::get_mod();
            if(built_mod()!=mod){
                _fact={1};
                _invfact={1};
                _invs={0};
                built_mod()=mod;
            }
        }
        if(n<(int)_fact.size())return;
        int old=_fact.size();
        _fact.resize(n+1);
        _invfact.resize(n+1);
        _invs.resize(n+1);
        if constexpr(BinomHasGetMod<mint>::value){
            auto mod=mint::get_mod();
            for(int i=old;i<=n;i++){
                _fact[i]=_fact[i-1]*i;
                if(i==1)_invs[i]=1;
                else _invs[i]=-_invs[mod%i]*(mod/i);
                _invfact[i]=_invfact[i-1]*_invs[i];
            }
        }else{
            for(int i=old;i<=n;i++){
                _fact[i]=_fact[i-1]*i;
                _invs[i]=mint(1)/i;
                _invfact[i]=_invfact[i-1]*_invs[i];
            }
        }
    }
    static mint fact(int i){
        assert(i>=0);
        build(i);
        return fact_table()[i];
    }
    static mint invfact(int i){
        assert(i>=0);
        build(i);
        return invfact_table()[i];
    }
    static mint inv(int i){
        assert(i>0);
        build(i);
        return invs_table()[i];
    }
    static mint C(int a,int b){//aCb
        if(b==0)return 1;
        if(a<0||b<0||a-b<0)return mint(0);
        build(a);
        auto&_fact=fact_table();
        auto&_invfact=invfact_table();
        return _fact[a]*_invfact[b]*_invfact[a-b];
    }
    static mint iC(int a,int b){//1/aCb
        if(a<0||b<0||a-b<0)return mint(0);
        build(a);
        auto&_fact=fact_table();
        auto&_invfact=invfact_table();
        return _fact[b]*_fact[a-b]*_invfact[a];
    }
    static mint P(int a,int b){
        if(a<b||b<0)return 0;
        build(a);
        auto&_fact=fact_table();
        auto&_invfact=invfact_table();
        return _fact[a]*_invfact[a-b];
    }
    static mint H(int a,int b){
        return C(a+b-1,b);
    }

};
template< typename T >
T extgcd(T a, T b, T &x, T &y) {
  T d = a;
  if(b != 0) {
    d = extgcd(b, a % b, y, x);
    y -= (a / b) * x;
  } else {
    x = 1;
    y = 0;
  }
  return d;
}
template<class T>
pair<T,T> inv(T x,T m){
    T a1,a2;
    T res=extgcd<ll>(x,m,a1,a2);
    T md=m/res;
    a1=(a1%md+md)%md;
    return {a1,md};
}
template<class T>
pair<T,T> mod_solve(T a,T b,T m){//return x s.t. ax=b mod m
    a%=m,b%=m;if(a<0)a+=m;if(b<0)b+=m;
    T g=gcd(gcd(a,b),m);
    a/=g,b/=g,m/=g;
    if(gcd(a,m)>1)return {-1,-1};
    return {(inv<ll>(a,m).first*b)%m,inv<ll>(a,m).second};
}

//x^2 ≡ b (mod p)
ll ModSqrt(ll b,ll p){
    static mt19937 mt(random_device{}());
    b%=p;if(b<0)b+=p;
    if(b==0)return 0;
    if(p==2){
        return b;
    }
    assert(p>=3);
    using mint=DynamicModInt<20260801>;mint::set_mod(p);
    if(mint(b).pow((p-1)/2)==-1){
        return -1;
    }
    if(p%4==3){
        return mint(b).pow((p+1)/4).val;
    }
    ll t=[&](){
        ll w;
        while(1){
            ll t=mt()%p;
            w=t*t-b;
            if(mint(w).pow((p-1)/2)==mint(-1)){
                return t;
            }
        }
        assert(0);
    }();
    mint w=mint(t*t-b);
    using T=pair<mint,mint>;
    auto ml=[&](T a,T b)->T{
        return T{a.first*b.first+a.second*b.second*w,a.second*b.first+a.first*b.second};
    };
    ll e=(p+1)/2;
    T ans={1,0};
    T gy={t,1};
    while(e){
        if(e%2)ans=ml(ans,gy);
        gy=ml(gy,gy);
        e/=2;
    }
    return ans.first.val;
}
#line 3 "lib/math/static-mod-int.hpp"
template<uint32_t mod>
struct StaticModInt{
    static_assert(0<mod&&mod<=(1u<<31)-1);
    using u32=uint32_t;
    using u64=uint64_t;
    u32 val;
    StaticModInt():val(0){}
    StaticModInt(ll x){
        ll v=x%mod;
        if(v<0)v+=mod;
        val=v;
    }
    constexpr static uint32_t get_mod(){
        return mod;
    }
    static StaticModInt raw(int v){
        assert(v>=0);
        StaticModInt mi;
        mi.val=v;
        return mi;
    }
    StaticModInt &operator+=(const StaticModInt&m){
        if((val+=m.val)>=mod)val-=mod;
        return *this;
    }
    StaticModInt &operator-=(const StaticModInt&m){
        if((val+=(mod-m.val))>=mod)val-=mod;
        return *this;
    }
    StaticModInt &operator*=(const StaticModInt&m){
        val=u64(val)*m.val%mod;
        return *this;
    }
    StaticModInt &operator/=(const StaticModInt&m){
        val=u64(val)*m.inv().val%mod;
        return *this;
    }
    StaticModInt operator-() const{
        return StaticModInt(mod-val);
    }
    StaticModInt operator+() const {
        return *this;
    }
    friend StaticModInt operator+(StaticModInt lhs, const StaticModInt& rhs){
        return lhs+=rhs;
    }
    friend StaticModInt operator-(StaticModInt lhs, const StaticModInt& rhs){
        return lhs-=rhs;
    }
    friend StaticModInt operator*(StaticModInt lhs, const StaticModInt& rhs){
        return lhs*=rhs;
    }
    friend StaticModInt operator/(StaticModInt lhs,const StaticModInt&rhs){
        return lhs/=rhs;
    }
    bool operator==(const StaticModInt&p) const{
        return p.val==val;
    }
    bool operator!=(const StaticModInt&p) const{
        return p.val!=val;
    }
    StaticModInt pow(int64_t n) const{
        StaticModInt res(1),mul(val);
        while(n){
            if(n%2)res*=mul;
            mul*=mul;
            n/=2;
        }
        return res;
    }

    friend ostream&operator<<(ostream&os,const StaticModInt&p){
        os<<p.val;
        return os;
    }
    friend istream&operator>>(istream&is,StaticModInt&p){
        int64_t x;
        is>>x;
        p=StaticModInt(x);
        return is;
    }
    StaticModInt inv()const{
        int64_t a=val,b=mod,u=1,v=0,t;
        #ifdef LOCAL
        assert(gcd(a,b)==1);
        #endif
        while(b>0){
            t=a/b;
            swap(a-=t*b,b);
            swap(u-=t*v,v);
        }
        return StaticModInt(u);
    }
};
#line 3 "lib/math/conv.hpp"
#ifndef ATCODER_CONVOLUTION_HPP
#define ATCODER_CONVOLUTION_HPP 1
#ifndef ATCODER_MATH_HPP
#define ATCODER_MATH_HPP 1

#include <algorithm>
#include <cassert>
#include <tuple>
#include <vector>
#ifndef ATCODER_INTERNAL_MATH_HPP
#define ATCODER_INTERNAL_MATH_HPP 1

#include <utility>

#ifdef _MSC_VER
#include <intrin.h>
#endif

namespace atcoder {

namespace internal {

// @param m `1 <= m`
// @return x mod m
constexpr long long safe_mod(long long x, long long m) {
    x %= m;
    if (x < 0) x += m;
    return x;
}

// Fast modular multiplication by BarrettReduction reduction
// Reference: https://en.wikipedia.org/wiki/Barrett_reduction
// NOTE: reconsider after Ice Lake
struct BarrettReduction {
    unsigned int _m;
    unsigned long long im;

    // @param m `1 <= m`
    explicit BarrettReduction(unsigned int m) : _m(m), im((unsigned long long)(-1) / m + 1) {}

    // @return m
    unsigned int umod() const { return _m; }

    // @param a `0 <= a < m`
    // @param b `0 <= b < m`
    // @return `a * b % m`
    unsigned int mul(unsigned int a, unsigned int b) const {
        // [1] m = 1
        // a = b = im = 0, so okay

        // [2] m >= 2
        // im = ceil(2^64 / m)
        // -> im * m = 2^64 + r (0 <= r < m)
        // let z = a*b = c*m + d (0 <= c, d < m)
        // a*b * im = (c*m + d) * im = c*(im*m) + d*im = c*2^64 + c*r + d*im
        // c*r + d*im < m * m + m * im < m * m + 2^64 + m <= 2^64 + m * (m + 1) < 2^64 * 2
        // ((ab * im) >> 64) == c or c + 1
        unsigned long long z = a;
        z *= b;
#ifdef _MSC_VER
        unsigned long long x;
        _umul128(z, im, &x);
#else
        unsigned long long x =
            (unsigned long long)(((unsigned __int128)(z)*im) >> 64);
#endif
        unsigned long long y = x * _m;
        return (unsigned int)(z - y + (z < y ? _m : 0));
    }
};

// @param n `0 <= n`
// @param m `1 <= m`
// @return `(x ** n) % m`
constexpr long long pow_mod_constexpr(long long x, long long n, int m) {
    if (m == 1) return 0;
    unsigned int _m = (unsigned int)(m);
    unsigned long long r = 1;
    unsigned long long y = safe_mod(x, m);
    while (n) {
        if (n & 1) r = (r * y) % _m;
        y = (y * y) % _m;
        n >>= 1;
    }
    return r;
}

// Reference:
// M. Forisek and J. Jancina,
// Fast Primality Testing for Integers That Fit into a Machine Word
// @param n `0 <= n`
constexpr bool is_prime_constexpr(int n) {
    if (n <= 1) return false;
    if (n == 2 || n == 7 || n == 61) return true;
    if (n % 2 == 0) return false;
    long long d = n - 1;
    while (d % 2 == 0) d /= 2;
    constexpr long long bases[3] = {2, 7, 61};
    for (long long a : bases) {
        long long t = d;
        long long y = pow_mod_constexpr(a, t, n);
        while (t != n - 1 && y != 1 && y != n - 1) {
            y = y * y % n;
            t <<= 1;
        }
        if (y != n - 1 && t % 2 == 0) {
            return false;
        }
    }
    return true;
}
template <int n> constexpr bool is_prime = is_prime_constexpr(n);

// @param b `1 <= b`
// @return pair(g, x) s.t. g = gcd(a, b), xa = g (mod b), 0 <= x < b/g
constexpr std::pair<long long, long long> inv_gcd(long long a, long long b) {
    a = safe_mod(a, b);
    if (a == 0) return {b, 0};

    // Contracts:
    // [1] s - m0 * a = 0 (mod b)
    // [2] t - m1 * a = 0 (mod b)
    // [3] s * |m1| + t * |m0| <= b
    long long s = b, t = a;
    long long m0 = 0, m1 = 1;

    while (t) {
        long long u = s / t;
        s -= t * u;
        m0 -= m1 * u;  // |m1 * u| <= |m1| * s <= b

        // [3]:
        // (s - t * u) * |m1| + t * |m0 - m1 * u|
        // <= s * |m1| - t * u * |m1| + t * (|m0| + |m1| * u)
        // = s * |m1| + t * |m0| <= b

        auto tmp = s;
        s = t;
        t = tmp;
        tmp = m0;
        m0 = m1;
        m1 = tmp;
    }
    // by [3]: |m0| <= b/g
    // by g != b: |m0| < b/g
    if (m0 < 0) m0 += b / s;
    return {s, m0};
}

// Compile time primitive root
// @param m must be prime
// @return primitive root (and minimum in now)
constexpr int primitive_root_constexpr(int m) {
    if (m == 2) return 1;
    if (m == 167772161) return 3;
    if (m == 469762049) return 3;
    if (m == 754974721) return 11;
    if (m == 998244353) return 3;
    int divs[20] = {};
    divs[0] = 2;
    int cnt = 1;
    int x = (m - 1) / 2;
    while (x % 2 == 0) x /= 2;
    for (int i = 3; (long long)(i)*i <= x; i += 2) {
        if (x % i == 0) {
            divs[cnt++] = i;
            while (x % i == 0) {
                x /= i;
            }
        }
    }
    if (x > 1) {
        divs[cnt++] = x;
    }
    for (int g = 2;; g++) {
        bool ok = true;
        for (int i = 0; i < cnt; i++) {
            if (pow_mod_constexpr(g, (m - 1) / divs[i], m) == 1) {
                ok = false;
                break;
            }
        }
        if (ok) return g;
    }
}
template <int m> constexpr int primitive_root = primitive_root_constexpr(m);

// @param n `n < 2^32`
// @param m `1 <= m < 2^32`
// @return sum_{i=0}^{n-1} floor((ai + b) / m) (mod 2^64)
unsigned long long floor_sum_unsigned(unsigned long long n,
                                      unsigned long long m,
                                      unsigned long long a,
                                      unsigned long long b) {
    unsigned long long ans = 0;
    while (true) {
        if (a >= m) {
            ans += n * (n - 1) / 2 * (a / m);
            a %= m;
        }
        if (b >= m) {
            ans += n * (b / m);
            b %= m;
        }

        unsigned long long y_max = a * n + b;
        if (y_max < m) break;
        // y_max < m * (n + 1)
        // floor(y_max / m) <= n
        n = (unsigned long long)(y_max / m);
        b = (unsigned long long)(y_max % m);
        std::swap(m, a);
    }
    return ans;
}

}  // namespace internal

}  // namespace atcoder

#endif  // ATCODER_INTERNAL_MATH_HPP

namespace atcoder {

long long pow_mod(long long x, long long n, int m) {
    assert(0 <= n && 1 <= m);
    if (m == 1) return 0;
    internal::BarrettReduction bt((unsigned int)(m));
    unsigned int r = 1, y = (unsigned int)(internal::safe_mod(x, m));
    while (n) {
        if (n & 1) r = bt.mul(r, y);
        y = bt.mul(y, y);
        n >>= 1;
    }
    return r;
}

long long inv_mod(long long x, long long m) {
    assert(1 <= m);
    auto z = internal::inv_gcd(x, m);
    assert(z.first == 1);
    return z.second;
}

// (rem, mod)
std::pair<long long, long long> crt(const std::vector<long long>& r,
                                    const std::vector<long long>& m) {
    assert(r.size() == m.size());
    int n = int(r.size());
    // Contracts: 0 <= r0 < m0
    long long r0 = 0, m0 = 1;
    for (int i = 0; i < n; i++) {
        assert(1 <= m[i]);
        long long r1 = internal::safe_mod(r[i], m[i]), m1 = m[i];
        if (m0 < m1) {
            std::swap(r0, r1);
            std::swap(m0, m1);
        }
        if (m0 % m1 == 0) {
            if (r0 % m1 != r1) return {0, 0};
            continue;
        }
        // assume: m0 > m1, lcm(m0, m1) >= 2 * max(m0, m1)

        // (r0, m0), (r1, m1) -> (r2, m2 = lcm(m0, m1));
        // r2 % m0 = r0
        // r2 % m1 = r1
        // -> (r0 + x*m0) % m1 = r1
        // -> x*u0*g = r1-r0 (mod u1*g) (u0*g = m0, u1*g = m1)
        // -> x = (r1 - r0) / g * inv(u0) (mod u1)

        // im = inv(u0) (mod u1) (0 <= im < u1)
        long long g, im;
        std::tie(g, im) = internal::inv_gcd(m0, m1);

        long long u1 = (m1 / g);
        // |r1 - r0| < (m0 + m1) <= lcm(m0, m1)
        if ((r1 - r0) % g) return {0, 0};

        // u1 * u1 <= m1 * m1 / g / g <= m0 * m1 / g = lcm(m0, m1)
        long long x = (r1 - r0) / g % u1 * im % u1;

        // |r0| + |m0 * x|
        // < m0 + m0 * (u1 - 1)
        // = m0 + m0 * m1 / g - m0
        // = lcm(m0, m1)
        r0 += x * m0;
        m0 *= u1;  // -> lcm(m0, m1)
        if (r0 < 0) r0 += m0;
    }
    return {r0, m0};
}

long long floor_sum(long long n, long long m, long long a, long long b) {
    assert(0 <= n && n < (1LL << 32));
    assert(1 <= m && m < (1LL << 32));
    unsigned long long ans = 0;
    if (a < 0) {
        unsigned long long a2 = internal::safe_mod(a, m);
        ans -= 1ULL * n * (n - 1) / 2 * ((a2 - a) / m);
        a = a2;
    }
    if (b < 0) {
        unsigned long long b2 = internal::safe_mod(b, m);
        ans -= 1ULL * n * ((b2 - b) / m);
        b = b2;
    }
    return ans + internal::floor_sum_unsigned(n, m, a, b);
}

}  // namespace atcoder

#endif  // ATCODER_MATH_HPP
#include <algorithm>
#include <array>
#include <cassert>
#include <type_traits>
#include <vector>

#ifndef ATCODER_INTERNAL_BITOP_HPP
#define ATCODER_INTERNAL_BITOP_HPP 1

#ifdef _MSC_VER
#include <intrin.h>
#endif

#if __cplusplus >= 202002L
#include <bit>
#endif

namespace atcoder {

namespace internal {

#if __cplusplus >= 202002L

using std::bit_ceil;

#else

// @return same with std::bit::bit_ceil
unsigned int bit_ceil(unsigned int n) {
    unsigned int x = 1;
    while (x < (unsigned int)(n)) x *= 2;
    return x;
}

#endif

// @param n `1 <= n`
// @return same with std::bit::countr_zero
int countr_zero(unsigned int n) {
#ifdef _MSC_VER
    unsigned long index;
    _BitScanForward(&index, n);
    return index;
#else
    return __builtin_ctz(n);
#endif
}

// @param n `1 <= n`
// @return same with std::bit::countr_zero
constexpr int countr_zero_constexpr(unsigned int n) {
    int x = 0;
    while (!(n & (1 << x))) x++;
    return x;
}

}  // namespace internal

}  // namespace atcoder

#endif  // ATCODER_INTERNAL_BITOP_HPP

namespace atcoder {

namespace internal {

template <class mint,
          int g = internal::primitive_root<mint::get_mod()>>
struct FastFourierTransformInfo {
    static constexpr int rank2 = countr_zero_constexpr(mint::get_mod() - 1);
    std::array<mint, rank2 + 1> root;   // root[i]^(2^i) == 1
    std::array<mint, rank2 + 1> iroot;  // root[i] * iroot[i] == 1

    std::array<mint, std::max(0, rank2 - 2 + 1)> rate2;
    std::array<mint, std::max(0, rank2 - 2 + 1)> irate2;

    std::array<mint, std::max(0, rank2 - 3 + 1)> rate3;
    std::array<mint, std::max(0, rank2 - 3 + 1)> irate3;

    FastFourierTransformInfo() {
        root[rank2] = mint(g).pow((mint::get_mod() - 1) >> rank2);
        iroot[rank2] = root[rank2].inv();
        for (int i = rank2 - 1; i >= 0; i--) {
            root[i] = root[i + 1] * root[i + 1];
            iroot[i] = iroot[i + 1] * iroot[i + 1];
        }

        {
            mint prod = 1, iprod = 1;
            for (int i = 0; i <= rank2 - 2; i++) {
                rate2[i] = root[i + 2] * prod;
                irate2[i] = iroot[i + 2] * iprod;
                prod *= iroot[i + 2];
                iprod *= root[i + 2];
            }
        }
        {
            mint prod = 1, iprod = 1;
            for (int i = 0; i <= rank2 - 3; i++) {
                rate3[i] = root[i + 3] * prod;
                irate3[i] = iroot[i + 3] * iprod;
                prod *= iroot[i + 3];
                iprod *= root[i + 3];
            }
        }
    }
};

template <class mint>
void butterfly(std::vector<mint>& a) {
    int n = int(a.size());
    int h = internal::countr_zero((unsigned int)n);

    static const FastFourierTransformInfo<mint> info;

    int len = 0;  // a[i, i+(n>>len), i+2*(n>>len), ..] is transformed
    while (len < h) {
        if (h - len == 1) {
            int p = 1 << (h - len - 1);
            mint rot = 1;
            for (int s = 0; s < (1 << len); s++) {
                int offset = s << (h - len);
                for (int i = 0; i < p; i++) {
                    auto l = a[i + offset];
                    auto r = a[i + offset + p] * rot;
                    a[i + offset] = l + r;
                    a[i + offset + p] = l - r;
                }
                if (s + 1 != (1 << len))
                    rot *= info.rate2[countr_zero(~(unsigned int)(s))];
            }
            len++;
        } else {
            // 4-base
            int p = 1 << (h - len - 2);
            mint rot = 1, imag = info.root[2];
            for (int s = 0; s < (1 << len); s++) {
                mint rot2 = rot * rot;
                mint rot3 = rot2 * rot;
                int offset = s << (h - len);
                for (int i = 0; i < p; i++) {
                    auto mod2 = 1ULL * mint::get_mod() * mint::get_mod();
                    auto a0 = 1ULL * a[i + offset].val;
                    auto a1 = 1ULL * a[i + offset + p].val * rot.val;
                    auto a2 = 1ULL * a[i + offset + 2 * p].val * rot2.val;
                    auto a3 = 1ULL * a[i + offset + 3 * p].val * rot3.val;
                    auto a1na3imag =
                        1ULL * mint(a1 + mod2 - a3).val * imag.val;
                    auto na2 = mod2 - a2;
                    a[i + offset] = a0 + a2 + a1 + a3;
                    a[i + offset + 1 * p] = a0 + a2 + (2 * mod2 - (a1 + a3));
                    a[i + offset + 2 * p] = a0 + na2 + a1na3imag;
                    a[i + offset + 3 * p] = a0 + na2 + (mod2 - a1na3imag);
                }
                if (s + 1 != (1 << len))
                    rot *= info.rate3[countr_zero(~(unsigned int)(s))];
            }
            len += 2;
        }
    }
}

template <class mint>
void butterfly_inv(std::vector<mint>& a) {
    int n = int(a.size());
    int h = internal::countr_zero((unsigned int)n);

    static const FastFourierTransformInfo<mint> info;

    int len = h;  // a[i, i+(n>>len), i+2*(n>>len), ..] is transformed
    while (len) {
        if (len == 1) {
            int p = 1 << (h - len);
            mint irot = 1;
            for (int s = 0; s < (1 << (len - 1)); s++) {
                int offset = s << (h - len + 1);
                for (int i = 0; i < p; i++) {
                    auto l = a[i + offset];
                    auto r = a[i + offset + p];
                    a[i + offset] = l + r;
                    a[i + offset + p] =
                        (unsigned long long)((unsigned int)(l.val - r.val) + mint::get_mod()) *
                        irot.val;
                    ;
                }
                if (s + 1 != (1 << (len - 1)))
                    irot *= info.irate2[countr_zero(~(unsigned int)(s))];
            }
            len--;
        } else {
            // 4-base
            int p = 1 << (h - len);
            mint irot = 1, iimag = info.iroot[2];
            for (int s = 0; s < (1 << (len - 2)); s++) {
                mint irot2 = irot * irot;
                mint irot3 = irot2 * irot;
                int offset = s << (h - len + 2);
                for (int i = 0; i < p; i++) {
                    auto a0 = 1ULL * a[i + offset + 0 * p].val;
                    auto a1 = 1ULL * a[i + offset + 1 * p].val;
                    auto a2 = 1ULL * a[i + offset + 2 * p].val;
                    auto a3 = 1ULL * a[i + offset + 3 * p].val;

                    auto a2na3iimag =
                        1ULL *
                        mint((mint::get_mod() + a2 - a3) * iimag.val).val;

                    a[i + offset] = a0 + a1 + a2 + a3;
                    a[i + offset + 1 * p] =
                        (a0 + (mint::get_mod() - a1) + a2na3iimag) * irot.val;
                    a[i + offset + 2 * p] =
                        (a0 + a1 + (mint::get_mod() - a2) + (mint::get_mod() - a3)) *
                        irot2.val;
                    a[i + offset + 3 * p] =
                        (a0 + (mint::get_mod() - a1) + (mint::get_mod() - a2na3iimag)) *
                        irot3.val;
                }
                if (s + 1 != (1 << (len - 2)))
                    irot *= info.irate3[countr_zero(~(unsigned int)(s))];
            }
            len -= 2;
        }
    }
}

template <class mint>
std::vector<mint> convolution_naive(const std::vector<mint>& a,
                                    const std::vector<mint>& b) {
    int n = int(a.size()), m = int(b.size());
    std::vector<mint> ans(n + m - 1);
    if (n < m) {
        for (int j = 0; j < m; j++) {
            for (int i = 0; i < n; i++) {
                ans[i + j] += a[i] * b[j];
            }
        }
    } else {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                ans[i + j] += a[i] * b[j];
            }
        }
    }
    return ans;
}

template <class mint>
std::vector<mint> convolution_fft(std::vector<mint> a, std::vector<mint> b) {
    int n = int(a.size()), m = int(b.size());
    int z = (int)internal::bit_ceil((unsigned int)(n + m - 1));
    a.resize(z);
    internal::butterfly(a);
    b.resize(z);
    internal::butterfly(b);
    for (int i = 0; i < z; i++) {
        a[i] *= b[i];
    }
    internal::butterfly_inv(a);
    a.resize(n + m - 1);
    mint iz = mint(z).inv();
    for (int i = 0; i < n + m - 1; i++) a[i] *= iz;
    return a;
}

}  // namespace internal

template <class mint>
std::vector<mint> convolution(std::vector<mint>&& a, std::vector<mint>&& b) {
    int n = int(a.size()), m = int(b.size());
    if (!n || !m) return {};

    int z = (int)internal::bit_ceil((unsigned int)(n + m - 1));
    assert((mint::get_mod() - 1) % z == 0);

    if (std::min(n, m) <= 60) return internal::convolution_naive(std::move(a), std::move(b));
    return internal::convolution_fft(std::move(a), std::move(b));
}
template <class mint>
std::vector<mint> convolution(const std::vector<mint>& a,
                              const std::vector<mint>& b) {
    int n = int(a.size()), m = int(b.size());
    if (!n || !m) return {};

    int z = (int)internal::bit_ceil((unsigned int)(n + m - 1));
    assert((mint::get_mod() - 1) % z == 0);

    if (std::min(n, m) <= 60) return internal::convolution_naive(a, b);
    return internal::convolution_fft(a, b);
}

template <unsigned int mod = 998244353,
          class T>
std::vector<T> convolution(const std::vector<T>& a, const std::vector<T>& b) {
    int n = int(a.size()), m = int(b.size());
    if (!n || !m) return {};

    using mint = StaticModInt<mod>;

    int z = (int)internal::bit_ceil((unsigned int)(n + m - 1));
    assert((mint::get_mod() - 1) % z == 0);

    std::vector<mint> a2(n), b2(m);
    for (int i = 0; i < n; i++) {
        a2[i] = mint(a[i]);
    }
    for (int i = 0; i < m; i++) {
        b2[i] = mint(b[i]);
    }
    auto c2 = convolution(std::move(a2), std::move(b2));
    std::vector<T> c(n + m - 1);
    for (int i = 0; i < n + m - 1; i++) {
        c[i] = c2[i].val;
    }
    return c;
}

std::vector<long long> convolution_ll(const std::vector<long long>& a,
                                      const std::vector<long long>& b) {
    int n = int(a.size()), m = int(b.size());
    if (!n || !m) return {};

    static constexpr unsigned long long MOD1 = 754974721;  // 2^24
    static constexpr unsigned long long MOD2 = 167772161;  // 2^25
    static constexpr unsigned long long MOD3 = 469762049;  // 2^26
    static constexpr unsigned long long M2M3 = MOD2 * MOD3;
    static constexpr unsigned long long M1M3 = MOD1 * MOD3;
    static constexpr unsigned long long M1M2 = MOD1 * MOD2;
    static constexpr unsigned long long M1M2M3 = MOD1 * MOD2 * MOD3;

    static constexpr unsigned long long i1 =
        internal::inv_gcd(MOD2 * MOD3, MOD1).second;
    static constexpr unsigned long long i2 =
        internal::inv_gcd(MOD1 * MOD3, MOD2).second;
    static constexpr unsigned long long i3 =
        internal::inv_gcd(MOD1 * MOD2, MOD3).second;
        
    static constexpr int MAX_AB_BIT = 24;
    static_assert(MOD1 % (1ull << MAX_AB_BIT) == 1, "MOD1 isn't enough to support an array length of 2^24.");
    static_assert(MOD2 % (1ull << MAX_AB_BIT) == 1, "MOD2 isn't enough to support an array length of 2^24.");
    static_assert(MOD3 % (1ull << MAX_AB_BIT) == 1, "MOD3 isn't enough to support an array length of 2^24.");
    assert(n + m - 1 <= (1 << MAX_AB_BIT));

    auto c1 = convolution<MOD1>(a, b);
    auto c2 = convolution<MOD2>(a, b);
    auto c3 = convolution<MOD3>(a, b);

    std::vector<long long> c(n + m - 1);
    for (int i = 0; i < n + m - 1; i++) {
        unsigned long long x = 0;
        x += (c1[i] * i1) % MOD1 * M2M3;
        x += (c2[i] * i2) % MOD2 * M1M3;
        x += (c3[i] * i3) % MOD3 * M1M2;
        // B = 2^63, -B <= x, r(real value) < B
        // (x, x - M, x - 2M, or x - 3M) = r (mod 2B)
        // r = c1[i] (mod MOD1)
        // focus on MOD1
        // r = x, x - M', x - 2M', x - 3M' (M' = M % 2^64) (mod 2B)
        // r = x,
        //     x - M' + (0 or 2B),
        //     x - 2M' + (0, 2B or 4B),
        //     x - 3M' + (0, 2B, 4B or 6B) (without mod!)
        // (r - x) = 0, (0)
        //           - M' + (0 or 2B), (1)
        //           -2M' + (0 or 2B or 4B), (2)
        //           -3M' + (0 or 2B or 4B or 6B) (3) (mod MOD1)
        // we checked that
        //   ((1) mod MOD1) mod 5 = 2
        //   ((2) mod MOD1) mod 5 = 3
        //   ((3) mod MOD1) mod 5 = 4
        long long diff =
            c1[i] - internal::safe_mod((long long)(x), (long long)(MOD1));
        if (diff < 0) diff += MOD1;
        static constexpr unsigned long long offset[5] = {
            0, 0, M1M2M3, 2 * M1M2M3, 3 * M1M2M3};
        x -= offset[diff % 5];
        c[i] = x;
    }

    return c;
}

}  // namespace atcoder

#endif  // ATCODER_CONVOLUTION_HPP
#line 3 "lib/poly/base.hpp"
#ifndef FPS_STRUCT
#define FPS_STRUCT
template<class mint>
struct fps:vc<mint>{
    #define BF atcoder::internal::butterfly
    #define IBF atcoder::internal::butterfly_inv
    using vc<mint>::vc;
    using vc<mint>::operator[];
    fps(const vc<mint>&v):vc<mint>(v){}
    fps&operator=(const vc<mint>&v){vc<mint>::operator=(v);return *this;}
    fps&operator+=(const fps&g){
        int n=g.size();
        if(n>(int)this->size())this->resize(n);
        for(int i=0;i<n;i++)(*this)[i]+=g[i];
        return *this;
    }
    fps&operator-=(const fps&g){
        int n=g.size();
        if(n>(int)this->size())this->resize(n);
        for(int i=0;i<n;i++)(*this)[i]-=g[i];
        return *this;
    }
    fps&operator*=(const fps&g){
        if(this->empty()||g.empty()){this->clear();return *this;}
        auto res=atcoder::convolution<mint>(*this,g);
        *this=fps(res.begin(),res.end());
        return *this;
    }
    fps& operator+=(const mint&v){if(this->empty())this->resize(1);(*this)[0]+=v;return *this;}
    fps& operator-=(const mint&v){if(this->empty())this->resize(1);(*this)[0]-=v;return *this;}
    fps& operator*=(const mint&v){for(auto& x:*this)x*=v;return *this;}
    fps& operator/=(const mint&v){mint inv=v.inv();for(auto& x:*this)x*=inv;return *this;}
    fps& operator>>=(const ll&v){*this=fps(this->begin()+min<int>(this->size(),v),this->end());return *this;} 
    fps& operator<<=(const ll&v){fps np(v+this->size());rep(i,this->size())np[i+v]=(*this)[i];*this=np;return *this;}
    fps operator+(const fps&g)const{return fps(*this)+=g;}
    fps operator-(const fps&g)const{return fps(*this)-=g;}
    fps operator*(const fps&g)const{return fps(*this)*=g;}
    fps operator+(const mint&v)const{return fps(*this)+=v;}
    friend fps operator+(const mint&v,const fps&g){return fps(g)+=v;}
    fps operator-(const mint&v)const{return fps(*this)-=v;}
    friend fps operator-(const mint&v,const fps&g){return -g+=v;}
    fps operator*(const mint&v)const{return fps(*this)*=v;}
    friend fps operator*(const mint&v,const fps&g){return fps(g)*=v;}
    fps operator/(const mint&v)const{return fps(*this)/=v;}
    friend fps operator/(const mint&v,const fps&g){return fps(g)/=v;}
    fps operator>>(const ll&v)const{return fps(*this)>>=v;}
    fps operator<<(const ll&v)const{return fps(*this)<<=v;}
    fps operator-()const{
        fps res=*this;
        for(auto&x:res)x=-x;
        return res;
    }
    fps pre(int sz)const{
        return fps(this->begin(),this->begin()+min((int)this->size(),sz));
    }
    //[0,n)
    fps inv(int deg=-1)const{
        assert((*this).size()&&(*this)[0].val!=0);
        if(deg==-1)deg=this->size();
        fps res{(*this)[0].inv()};
        for(int len=1;len<deg;len*=2){
            int nxt=len*2;
            auto F=(*this).pre(len*2);
            auto res2=res;
            F.resize(nxt);
            res2.resize(nxt);
            BF(F);
            BF(res2);
            rep(i,res2.size())F[i]*=res2[i];
            IBF(F);
            mint inv=1/mint(nxt);F*=inv;
            rep(i,len)F[i]=0;REP(i,len,len*2)F[i]=-F[i];
            BF(F);
            rep(i,res2.size())F[i]*=res2[i];
            IBF(F);
            F*=inv;
            res.resize(len*2);
            REP(i,len,len*2)res[i]=F[i];
        }
        res.resize(deg);
        return res;
    }
    fps diff()const{
        auto res=*this;
        rep(i,res.size())res[i]*=i;
        if(res.size())res.erase(res.begin());
        return res;
    }
    fps integral()const{
        fps res(this->size()+1);
        rep(i,this->size())res[i+1]=(*this)[i]*Binom<mint>::inv(i+1);
        return res;
    }
    fps log(int deg=-1)const{
        if(deg==-1)deg=this->size();
        assert(this->size()&&(*this)[0]==1);
        return (this->diff().pre(deg)*this->inv(deg)).integral().pre(deg);
    }
    fps exp(int deg=-1)const{
        assert((*this).size()&&(*this)[0]==0); 
        if(deg==-1)deg=this->size();
        fps res{1},h{1};
        for(int len=1;len<deg;len*=2){
            int nxt=len*2;
            mint inv=1/mint(nxt);
            fps G=res;G.resize(nxt);BF(G);
            fps H=h;H.resize(nxt);BF(H);
            fps V=G;
            rep(i,nxt)V[i]*=H[i];
            IBF(V);rep(i,nxt)V[i]*=inv;
            rep(i,len)V[i]=0;REP(i,len,nxt)V[i]=-V[i];
            BF(V);
            fps g_prime(nxt);
            rep(i,len-1)g_prime[i]=res[i+1]*(i+1);
            BF(g_prime);
            fps f_prime(nxt);
            rep(i,len)if(i+1<(int)this->size())f_prime[i]=(*this)[i+1]*(i+1);
            BF(f_prime);
            fps w_prime(nxt);
            rep(i,nxt)w_prime[i]=g_prime[i]*H[i]+f_prime[i]*V[i];
            IBF(w_prime);w_prime*=inv;
            DREP(i,nxt-1,len){
                mint fi=(i<(int)this->size())?(*this)[i]:0;
                w_prime[i]=fi-w_prime[i-1]*Binom<mint>::inv(i);
            }
            rep(i,len)w_prime[i]=0;
            BF(w_prime);
            rep(i,nxt)G[i]*=w_prime[i];
            IBF(G);G*=inv;
            res.resize(nxt);
            REP(i,len,nxt)res[i]=G[i];
            if(nxt<deg){
                rep(i,nxt)H[i]*=(V[i]-w_prime[i]);
                IBF(H);rep(i,nxt)H[i]*=inv;
                h.resize(nxt);
                REP(i,len,nxt)h[i]=H[i];
            }
        }
        res.resize(deg);
        return res;
    }
    fps pow(ll m,int deg=-1)const{
        if(deg==-1)deg=this->size();
        if(m==0){fps res(deg);res[0]=1;return res;}
        rep(i,this->size()){
            if(m*i>=deg)return fps(deg);
            if((*this)[i].val){
                auto target=(*this)>>i;
                mint coef=mint((*this)[i]);
                mint inv=1/coef;
                target*=inv; 
                return ((m*target.log(deg-i*m)).exp(deg-i*m)*mint(coef).pow(m))<<(i*m);
            }
        }
        return fps(deg);
    }
    void extend(int n){
        if(this->size()<n)this->resize(n);
    }
    fps shrink(){
        auto res=(*this);
        while(res.size()&&res.back()==0)res.pop_back();
        return res;
    }
    pair<fps,fps>div(fps f)const{
        if(this->size()<f.size()){ 
            return {{},(*this)}; 
        }
        int qsize=this->size()-f.size()+1;
        auto rthis=*this;reverse(all(rthis));
        auto rf=f;reverse(all(rf));
        auto rq=(rthis.pre(qsize)*rf.inv(qsize)).pre(qsize);
        reverse(all(rq));
        return {rq,(*this-rq*f).shrink()};
    }
    fps div_only(fps f)const{
        if(this->size()<f.size()){ 
            return {{}}; 
        }
        int qsize=this->size()-f.size()+1;
        auto rthis=*this;reverse(all(rthis));
        auto rf=f;reverse(all(rf));
        auto rq=(rthis.pre(qsize)*rf.inv(qsize)).pre(qsize);
        reverse(all(rq));
        return rq;
    }
    
    optional<fps> sqrt(int deg=-1)const{
        if(deg==-1)deg=this->size();
        if((this)->size()==0)return fps(deg);
        if((*this)[0]==0){
            REP(i,1,this->size()){
                if((*this)[i].val){
                    if(i%2)return nullopt; 
                    if(deg-i/2<=0)break;
                    auto res=((*this)>>i).sqrt(deg-i/2);
                    if(!res.has_value())return nullopt;
                    auto ret=res.value();
                    ret<<=(i/2);
                    ret.resize(deg);
                    return ret;
                }
            }
            return fps(deg);
        }
        ll s=ModSqrt((*this)[0].val,mint::get_mod());
        if(s==-1)return nullopt;
        fps res{s};
        mint inv=1/mint(2);
        for(int i=1;i<deg;i*=2){
            res=(res+(*this).pre(i<<1)*res.inv(i<<1))*inv;
        }
        return res.pre(deg);
    }
    //f(x) -> f(vx)
    fps substitute(mint v){
        mint coef=1;
        fps nxt(*this);rep(i,nxt.size()){
            nxt[i]*=coef;
            coef*=v;
        }
        return nxt;
    }
};
#endif
#line 5 "A/main.cpp"
using mint=StaticModInt<998244353>;
using B=Binom<mint>;
//x ≡ r0.first (mod r0.second)
//x ≡ r1.first (mod r1.second)
template<class T>
pair<T,T>CRT(pair<T,T>r0,pair<T,T>r1){
    r0.second=abs(r0.second),r1.second=abs(r1.second);
    r0.first%=r0.second;if(r0.first<0)r0.first+=r0.second;
    r1.first%=r1.second;if(r1.first<0)r1.first+=r1.second;
    auto GCD=[&](T a,T b){
        while(b){a%=b;swap(a,b);}return a;
    };
    if(r0.second==r1.second){
        if(r0.first==r1.first)return r0;
        return{-1,-1};
    }
    T right=r1.first-r0.first;
    T g=GCD(r0.second,r1.second);
    T mod=r0.second/g*r1.second;
    if(right%g)return {-1,-1};
    right/=g;
    r0.second/=g;
    r1.second/=g;
    T x,y;
    extgcd<T>(r0.second,r1.second,x,y);
    x*=right;x%=r1.second;if(x<0)x+=r1.second;
    return {(r0.second*g*x+r0.first)%mod,mod};
}
template<class T>
pair<T,T>CRT(vc<pair<T,T>>vs){
    assert(vs.size());
    pair<T,T>ans={vs[0]};
    REP(i,1,vs.size()){
        auto res=CRT(vs[i],ans);
        if(res.first==-1)return res;
        ans=res;
    }
    return ans;
}
void solve(){
    LL(A,B,a,b);
    auto ans=CRT(pll{a,A},{b,B});
    PRT(ans.first);
}
signed main(){
    int t=1;
    // cin >> t;
    while(t--)solve();
}
0