#include #include using namespace std; using mint=atcoder::modint998244353; using ll=long long; #define rep(i,l,r)for(int i=(l);i<(r);i++) mint eval(vector&f,mint x){ mint ans=0; for(int i=(int)f.size()-1;i>=0;i--)ans=ans*x+f[i]; return ans; } vector newton_interpolation(vector&x,vector&y){ vectorret; vectorf({1}); rep(i,0,x.size()){ mint e=eval(f,x[i]); mint c=eval(ret,x[i]); mint coef = (y[i]-c)/e; ret.push_back(0); rep(j,0,f.size())ret[j]+=coef*f[j]; for(int j=(int)f.size()-1;j>0;j--)f[j]=f[j]*-x[i]+f[j-1]; f[0]*=-x[i]; f.push_back(1); } while(ret.back()==0)ret.pop_back(); return ret; } vector>S; vectorfact(25),invfact(25); void precalc(){ rep(i,0,25){ vectorx({0}); vectory({0}); rep(k,1,30){ x.push_back(k); y.push_back(y.back()+mint(k-1).pow(i)); } S.push_back(newton_interpolation(x,y)); } fact[0]=invfact[0]=1; rep(i,1,25){ fact[i]=fact[i-1]*i; invfact[i]=invfact[i-1]/i; } } mint choose(int n,int p,int q){ return fact[n]*invfact[p]*invfact[q]*invfact[n-p-q]; } void solve(){ int p,q,M,A; ll N,B; cin >> p >> q >> N >> M >> A >> B; N++; mint ans=0; unordered_map>memo_main; memo_main[p][q]=1; while(N>0 && memo_main.size()>0){ unordered_map>memo_sub; int a1=A/M;A%=M; if(A<0)A+=M,a1--; ll b1=B/M;B%=M; if(B<0)B+=M,b1--; for(auto[p,qv]:memo_main)for(auto[q,v]:qv){ rep(r,0,q+1)rep(s,0,q-r+1){ mint coef = choose(q,r,s) * mint(a1).pow(s) * mint(b1).pow(q-r-s); if(coef!=0)memo_sub[p+s][r]+=coef*v; } } memo_main.clear(); ll K=(A*(N-1)+B)/M, Kr=(A*(N-1)+B)%M; if(Kr<0)K--; for(auto[p,qv]:memo_sub)for(auto[q,v]:qv){ if(q==0){ ans+=eval(S[p],N)*v; }else if(K!=0){ ans+=mint(K).pow(q)*eval(S[p],N)*v; rep(i,0,q)rep(j,0,p+2){ mint coef=choose(q,i,0)*S[p][j]; if(coef!=0){ memo_main[i][j]-=coef*v; } } } } B=M-B+A-1; N=K; swap(A,M); } cout << ans.val() << endl; } int main(){ precalc(); int t; cin >> t; while(t--)solve(); }