#include namespace { #pragma GCC diagnostic ignored "-Wunused-function" #include #pragma GCC diagnostic warning "-Wunused-function" using namespace std; using namespace atcoder; #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) template bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; } template bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; } using ll = long long; using P = pair; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; using mint = modint998244353; vector power_sums(auto n, int k) { vector fact(k+1), ifact(k+1); fact[0] = 1; rep(i, k) fact[i+1] = fact[i] * (i+1); ifact[k] = fact[k].inv(); rrep(i, k) ifact[i] = ifact[i+1] * (i+1); vector f(k); mint nm = n, n2i = 1; // (e^nx-1)/x rep(i, k) n2i *= nm, f[i] = ifact[i+1] * n2i; // (e^x-1)/x rep(i, k) { for (int j = 1; i + j < k; j++) { f[i+j] -= f[i] * ifact[j+1]; } f[i] *= fact[i]; } return f; } auto solve(int p, int q, int n, int m, int a, int b) { struct Comb { mint d[31][31]; mint inv[31][31]; Comb() { d[0][0] = 1; rep(i, 30) rep(j, i + 1) d[i+1][j] += d[i][j], d[i+1][j+1] += d[i][j]; rep(i, 31) rep(j, i + 1) inv[i][j] = d[i][j].inv(); } mint operator()(int n, int k) { return d[n][k]; } }; static Comb C; assert (n >= 0); if (n == 0) return mint(); n--; mint ans = mint(0).pow(p) * mint(b >= 0 ? b / m : (b - (m - 1)) / m).pow(q); // (0, n] auto to_floor = [&](vector>& d, int n) { auto sms = power_sums(n + 1, p + q + 1); sms[0]--; // 0^0 for (int i = 0; i <= p + q; i++) { // x^p (y^i-(y-1)^i) int qmx = p + q - i; d[i].resize(qmx + 1 + 1); for (int j = qmx + 1; j > 0; j--) { d[i][j] = 0; for (int dj = 1; dj <= j; dj++) { mint v = C(j, dj) * d[i][j-dj]; d[i][j] += dj % 2 ? v : -v; } } d[i][0] = sms[i]; } return d; }; auto to_ij = [&](vector>& d) { for (int i = 0; i <= p + q; i++) { // if (i == 0) {for (auto x : d[i]) cout << x.val() << ' '; cout << endl;} int qmx = p + q - i; for (int j = 0; j <= qmx; j++) { d[i][j] = d[i][j+1] * C.inv[j + 1][j]; for (int dj = 2; j + dj <= qmx + 1; dj++) { mint v = C(j + dj, dj) * d[i][j]; d[i][j+dj] += dj % 2 == 0 ? v : -v; } } d[i].resize(qmx + 1); } }; auto extract_line = [&](int a, ll b, int m, int n) { // a/m = a1/m + a2 // a=a1+a2m int a1 = a % m, a2 = a / m; if (a1 > 0) a1 -= m, a2++; ll fn = (ll)a1 * n + b; // fn/m = fn1/m + b2 // fn = fn1 + b2m ll fn1 = fn % m, b2 = fn / m; if (fn1 < 0) fn1 += m, b2--; ll b1 = b - b2 * m; // (a1x+b1)/m + (a2x+b2) assert(a1 + a2 * m == a && b1 + b2 * m == b && -m < a1 && a1 <= 0 && 0 <= (ll)a1*n+b1 && (ll)a1*n+b1 < m); return tuple(a1, a2, b1, b2); }; auto rec = [&](auto&& self, int a, int b, ll m) -> vector> { // <= p+q if (a < b) { auto res = self(self, b, a, m); rep(i, p + q + 1) rep(j, p + q + 1 - i) if (i < j) swap(res[i][j], res[j][i]); return res; } assert(a > 0); // ax+by=m y=(-ax+m)/b int xmx = m / a; if (xmx == 0) { vector> res(p + q + 1); rep(i, p + q + 1) res[i].resize(p + q + 1 - i); return res; } assert(b > 0); auto [a1, a2, b1, b2] = extract_line(-a, m, b, xmx); // cout << "X"<; using param_type = dist_type::param_type; int RI(int L, int R) { assert(L < R); return dist_type(L, R - 1)(gen); } auto floor_div(signed_integral auto x, signed_integral auto y) { return x / y - ((x ^ y) < 0 && x % y != 0); } template T floor_div(T x, unsigned_integral auto y) { return x >= 0 ? T(x / y) : -T(-x / y + (-x % y != 0)); } auto ceil_div(signed_integral auto x, signed_integral auto y) { return x / y + ((x ^ y) >= 0 && x % y != 0); } template T ceil_div(T x, unsigned_integral auto y) { return x >= 0 ? T(x / y + (x % y != 0)) : -T(-x / y); } } int main() { ios::sync_with_stdio(false); cin.tie(0); // rep(_, 1000) { // int p = RI(0, 10), q = RI(0, 10); // int n = RI(0, 200); // int a = RI(-1e9, 1e9), b = RI(-1e9, 1e9), m = RI(1, 1e9); // // tie(p,q,n,m,a,b)=tuple(0,2,2,2,1,1); // mint res = solve(p, q, n + 1, m, a, b); // mint res_naive; // for (int i = 0; i <= n; i++) { // res_naive += mint(i).pow(p) * mint(floor_div((ll)a * i + b, m)).pow(q); // } // if (res != res_naive) { // cout << p << ' ' << q << ' '<< n << ' ' << m << ' ' << a << ' ' << b << endl; // cout << res.val() << endl; // cout << res_naive.val() << endl; // exit(0); // } // } // cout << "ok" << endl; // exit(0); int tt; cin >> tt; while (tt--) { int p, q, n, m, a, b; cin >> p >> q >> n >> m >> a >> b; cout << solve(p, q, n + 1, m, a, b).val() << '\n'; } }