#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
const int MOD = 998244353;
const int INF = 0x3f3f3f3f;
int vis[MAXN], dat[MAXN], pos[MAXN], cnt[MAXN];
inline int read() {
  int x = 0;
  char ch = getchar();
  while (!isdigit(ch)) ch = getchar();
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x;
}
template <typename T1, typename T2>
inline void Max(T1 &x, T2 y) {
  if (x < y) x = y;
}
template <typename T1, typename T2>
inline void Min(T1 &x, T2 y) {
  if (x > y) x = y;
}
int main() {
  int t = read();
  while (t--) {
    int n = read(), k = read();
    for (int i = 0; i <= n + 1; i++) vis[i] = 0;
    vis[0] = vis[n + 1] = INF;
    for (int i = 1; i <= n; i++) dat[i] = read(), pos[dat[i]] = i;
    for (int i = 1; i <= k; i++) {
      int x = read();
      vis[pos[x]] = i, cnt[i] = pos[x];
    }
    int ans = 1;
    for (int i = 1; i <= k; i++)
      ans *= ((vis[cnt[i] - 1] < i) + (vis[cnt[i] + 1] < i)), ans %= MOD;
    printf("%d\n", ans);
  }
  return 0;
}
