#include #include #include #include #include #include #include #include #include #include #include #define LL long long #define ULL unsigned long long #define pii pair #define xx first #define yy second #define ls rt << 1 #define rs rt << 1 | 1 #define lson ls, l, m #define rson rs, m + 1, r using namespace std; const int mod = 998244353, N = 100100; int c[N], b[N]; int T, k, n, m; int pm (LL x, int y) { LL r = 1; while (y) { if (y & 1) r *= x, r %= mod; x *= x, x %= mod; y >>= 1; } return r; } int solve () { if (b[1] != c[1]) return 0; LL res = 1; for (int i = 2; i <= n; i++) { if (b[i] == b[i - 1] && c[i] == c[i - 1]) { // cout << i << ' ' << c[i] - b[i] - i + 2 << endl; if (c[i] - b[i] - i + 2 <= 0) return 0; res *= c[i] - b[i] - i + 2; res %= mod; } else { if (b[i] != b[i - 1] && c[i] != c[i - 1]) return 0; if (b[i] > b[i - 1]) return 0; if (c[i] < c[i - 1]) return 0; } } return res; } int main () { // freopen ("in.txt", "r", stdin); cin >> T; while (T--) { // int n; cin >> n; for (int i = 1; i <= n; i++) { scanf ("%d", &b[i]); } for (int i = 1; i <= n; i++) { scanf ("%d", &c[i]); } cout << solve () << endl; } }