#include <iostream>
#include <vector>
#include <string>
#include <cstring>

#define int long long

using namespace std;

const int MOD = 998244353;

int n;
vector<int> a;

vector<int> mem;
int g(int x) {
    if (mem[x] != -1) return mem[x];
    if (x == 0) return mem[x] = 0;
    return mem[x] = (3 * g(x - 1) + 2) % MOD;
}


int f(int x, int pos) {
    if (x == 0) return 0;
    if (a[x - 1] == pos) {
        return f(x - 1, pos);
    } else if (a[x - 1] == 1) {
        return (g(x - 1) + 1 + f(x - 1, 2 - pos)) % MOD;
    } else {
        return (2 * g(x - 1) + 2 + f(x - 1, pos)) % MOD;
    }
}

void solve() {
    cin >> n;
    a.resize(n);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        a[i]--;
    }
    mem.assign(n + 1, -1);

    int ans1 = f(n, 0);
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
        a[i]--;
    }
    int ans2 = f(n, 0);

//    cerr << ans1 << ' '  << ans2 << endl;
    cout << llabs(ans1 - ans2) % MOD << endl;
}

signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int t = 1;
    cin >> t;
    while(t--){
        solve();
    }
}