#include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define getmid(l,r) ((l) + ((r) - (l)) / 2) #define MEM(a,b) memset(a,b,sizeof(a)) #define MP(a,b) make_pair(a,b) #define PB push_back #define scan(x) scanf("%d",&x) typedef long long ll; typedef pair pii; const double eps = 1e-8; const int INF = (1 << 30) - 1; const ll mod = 998244353; const int MAXN = 200010; int T,n; int B[MAXN],C[MAXN]; int main(){ scanf("%d",&T); while(T--){ scanf("%d",&n); for(int i = 1; i <= n; ++i) scanf("%d",&B[i]); for(int i = 1; i <= n; ++i) scanf("%d",&C[i]); if(n == 1){ if(B[1] == C[1]) printf("1\n"); else printf("0\n"); continue; } bool f = true; for(int i = 2; i <= n; ++i){ if(B[i] > B[i - 1]) f = false; if(C[i] < C[i - 1]) f = false; if(B[i] != B[i - 1] && C[i] != C[i - 1]) f = false; if(!f) break; } if(!f){ printf("0\n"); continue; } ll ans = 1; int L = B[1],R = B[1]; for(int i = 2; i <= n; ++i){ if(B[i] != B[i - 1]) continue; if(C[i] != C[i - 1]) continue; ans = ans * (C[i] - B[i] - i + 2) % mod; } printf("%I64d\n",ans); } return 0; }