#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 5;
const int mod = 998244353;
int t, n, k, a[maxn], b[maxn];
int pos[maxn], del[maxn];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> t;
  while (t--) {
    cin >> n >> k;
    for (int i = 1; i <= n; i++) del[i] = 0;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
      pos[a[i]] = i;
    }
    for (int i = 1; i <= k; i++) {
      cin >> b[i];
      del[b[i]] = i;
    }
    long long res = 1;
    for (int i = 1; i <= k; i++) {
      if (pos[b[i]] == 1) {
        if (del[a[2]] > i) {
          res = 0;
          break;
        }
      } else if (pos[b[i]] == n) {
        if (del[a[n - 1]] > i) {
          res = 0;
          break;
        }
      } else {
        int r = 0;
        if (del[a[pos[b[i]] - 1]] < i) r++;
        if (del[a[pos[b[i]] + 1]] < i) r++;
        res = res * r % mod;
      }
    }
    cout << res << "\n";
  }
}
