#include <bits/stdc++.h>
using namespace std;
int n, k;
int a[200020], b[200020], vis[200020], fa[2][200020], ans, rk[200020],
    ed[200020], Ans[200020];
int find(int f, int x) {
  return x != fa[f][x] && ed[a[x]] ? fa[f][x] = find(f, fa[f][x]) : x;
}
inline void solve() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; ++i)
    scanf("%d", &a[i]), vis[i] = ed[i] = 0, rk[a[i]] = i;
  Ans[0] = 1, ans = 0;
  for (int i = 1; i <= n; ++i) Ans[i] = 2ll * Ans[i - 1] % 998244353;
  for (int i = 1; i <= k; ++i) scanf("%d", &b[i]), vis[b[i]] = 1;
  for (int i = 2; i <= n; ++i) fa[0][i] = i - 1;
  for (int i = 1; i < n; ++i) fa[1][i] = i + 1;
  fa[0][1] = 1, fa[1][n] = n;
  for (int i = 1; i <= k; ++i) {
    int l = find(0, fa[0][rk[b[i]]]);
    int r = find(1, fa[1][rk[b[i]]]);
    if (vis[a[l]] + vis[a[r]] == 0) ++ans;
    if (vis[a[l]] + vis[a[r]] > 1) {
      puts("0");
      return;
    }
    ed[b[i]] = 1;
  }
  printf("%d\n", Ans[ans]);
}
int main() {
  int T = 1;
  scanf("%d", &T);
  while (T--) solve();
}
