#include #include #include #include #include using namespace std; const int MaxN = 1e5, Pt = 998244353; int T, n; long long Min[MaxN + 5], Max[MaxN + 5]; int main() { scanf("%d", &T); while(T--) { long long sum = 1; bool flag = 1; scanf("%d", &n); for(int i = 1; i <= n; i++) scanf("%I64d", &Min[i]); for(int i = 1; i <= n; i++) scanf("%I64d", &Max[i]); if(Min[1] != Max[1]) flag = 0; else { long long ans_Min = Min[1]; long long ans_Max = Max[1]; for(int i = 2; i <= n; i++) { if(Min[i] > Max[i]) {flag = 0; break;} if(Min[i] != ans_Min && Max[i] != ans_Max) {flag = 0; break;} if(Min[i] > ans_Min || Max[i] < ans_Max) {flag = 0; break;} if(Min[i] == ans_Min && Max[i] == ans_Max) sum = (sum * (ans_Max - ans_Min + 2 - i)) % Pt; if(Min[i] == ans_Min && Max[i] > ans_Max) ans_Max = Max[i]; if(Min[i] < ans_Min && Max[i] == ans_Max) ans_Min = Min[i]; } } if(flag == 0) printf("0\n"); else printf("%I64d\n", sum); } }