#include <bits/stdc++.h>
using namespace std;
int lft[200005], rght[200005], val[200005], ind[200005], b[200005];
bool vis[200005];
int main() {
  int ts;
  scanf("%d", &ts);
  while (ts--) {
    int n, k;
    scanf("%d %d", &n, &k);
    for (int i = 0; i <= n + 1; i++) val[i] = 0, vis[i] = 0;
    for (int i = 1; i <= n; i++)
      lft[i] = i - 1, rght[i] = i + 1, scanf("%d", &val[i]), ind[val[i]] = i;
    for (int i = 0; i < k; i++) scanf("%d", &b[i]), vis[b[i]] = 1;
    for (int i = 1; i <= n; i++)
      if (!vis[i]) val[ind[i]] = -1;
    int res = 1ll;
    for (int i = 0; i < k; i++) {
      int p = ind[b[i]];
      int x = lft[p];
      int y = rght[p];
      if (val[x] != -1 && val[y] != -1) {
        res = 0;
        break;
      } else {
        res = (res * (1 + (val[x] == -1 && val[y] == -1))) % 998244353;
        rght[x] = y;
        lft[y] = x;
      }
    }
    printf("%d\n", res);
  }
  return 0;
}
