#include <bits/stdc++.h>
using namespace std;
const double pi = 3.1415926535897;
int m, n, t, L[200006], R[200006], yy[200006], sy[200005], vis[200005],
    mod = 998244353;
signed main() {
  std::ios::sync_with_stdio(0);
  std::cin.tie(0);
  cin >> t;
  vis[0] = 1;
  while (t--) {
    cin >> m >> n;
    for (int i = 1; i <= m; ++i) cin >> yy[i];
    for (int i = 1; i <= m; ++i) {
      L[yy[i]] = yy[i - 1];
      R[yy[i]] = yy[i + 1];
    }
    for (int i = 1; i <= n; ++i) {
      cin >> sy[i];
      vis[sy[i]] = 1;
    }
    int ans = 1;
    for (int i = 1; i <= n; ++i) {
      vis[sy[i]] = 0;
      if (!vis[L[sy[i]]] && !vis[R[sy[i]]]) {
        ans = (ans << 1) % mod;
        L[R[R[sy[i]]]] = sy[i];
        R[sy[i]] = R[R[sy[i]]];
      } else if (!vis[L[sy[i]]]) {
        R[L[L[sy[i]]]] = sy[i];
        L[sy[i]] = L[L[sy[i]]];
      } else if (!vis[R[sy[i]]]) {
        L[R[R[sy[i]]]] = sy[i];
        R[sy[i]] = R[R[sy[i]]];
      } else {
        ans = 0;
        break;
      }
    }
    cout << ans << endl;
    for (int i = 1; i <= m; ++i) vis[i] = yy[i] = 0;
    L[yy[1]] = R[yy[m]];
  }
  return 0;
}
