#include #include #include #include #include #include #include #include #include #include #include #include #include #include #define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl; #define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using namespace std; typedef long long ll; template vector> vec2d(int n, int m, T v){ return vector>(n, vector(m, v)); } template vector>> vec3d(int n, int m, int k, T v){ return vector>>(n, vector>(m, vector(k, v))); } template void print_vector(vector v, char delimiter=' '){ if(v.empty()) { cout << endl; return; } for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter; cout << v.back() << endl; } using mint = atcoder::modint998244353; ostream& operator<<(ostream& os, const mint& m){ os << m.val(); return os; } template class Matrix { public: array, N> dat; Matrix(T val=0) { for(int i = 0; i < N; i++){ for(int j = 0; j < M; j++){ dat[i][j] = val; } } } Matrix(array, N> dat): dat(dat){ } array& operator[](int x) { return dat[x]; } }; template Matrix operator*(Matrix a, Matrix b){ Matrix c(T(0)); for(int i = 0; i < N; i++){ for(int j = 0; j < K; j++){ for(int k = 0; k < M; k++){ c.dat[i][j] += a.dat[i][k]*b.dat[k][j]; } } } return c; } template Matrix operator^(Matrix m, long long r){ Matrix ans(T(0)); for(int i = 0; i < N; i++) ans[i][i] = T(1); while (r > 0) { if (r&1) ans = (ans*m); m = (m*m); r >>= 1; } return ans; } template void print_mat(Matrix a){ for(int i = 0; i < N; i++){ for(int j = 0; j < M; j++){ cout << a.dat[i][j] << ' '; } cout << endl; } } template ostream& operator<<(ostream& os, const Matrix& m){ print_mat(m); return os; } mint naive(ll n, ll m){ if(n > m) swap(n, m); if(n == 0) return mint(1); vector dp(n+m+1); dp[0] = 1; dp[1] = 2; for(ll x = 2; x <= n+m; x++){ if(x <= n){ dp[x] += dp[x-1]*2*x; dp[x] += dp[x-2]*(x-1); }else if(x <= m){ dp[x] += dp[x-1]*(2*n+1); dp[x] += dp[x-2]*n; }else{ int c = n+m-x+1; dp[x] += dp[x-1]*2*c; dp[x] += dp[x-2]*c; } } // print_vector(dp); return dp[n+m]; } const int N = 10000000; using M = Matrix; using V = Matrix; M f0[N+1]; M f1[N+1]; void init(){ f0[1] = M({{ {mint(1), mint(0)}, {mint(0), mint(1)}, }}); for(int x = 2; x <= N; x++){ f0[x] = M({{ {mint(2*x), mint(x-1)}, {mint(1), mint(0)}, }})*f0[x-1]; } f1[0] = M({{ {mint(1), mint(0)}, {mint(0), mint(1)}, }}); for(int x = 1; x <= N; x++){ f1[x] = f1[x-1]*M({{ {mint(2*x), mint(x)}, {mint(1), mint(0)}, }}); } } mint solve(ll n, ll m){ if(n > m) swap(n, m); if(n == 0) return mint(1); mint dp1 = 2; mint dp0 = 1; V v({{ {dp1}, {dp0} }}); v = f0[n]*v; M A({{ {mint(2*n+1), mint(n)}, {mint(1), mint(0)}, }}); v = (A^(m-n))*v; v = f1[n]*v; return v[0][0]; } int main(){ ios::sync_with_stdio(false); cin.tie(0); cout << setprecision(10) << fixed; init(); int t; cin >> t; while(t--) { ll n, m; cin >> n >> m; cout << solve(n, m) << endl; } }