#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 5;
const int M = 1e7 + 5;
const int inf = 0x3f3f3f3f;
const long long mod = 998244353;
const double eps = 1e-8;
const long double pi = acos(-1.0L);
long long read() {
  long long x = 0, t = 1;
  char ch;
  while (!isdigit(ch = getchar()))
    if (ch == '-') t = -1;
  while (isdigit(ch)) {
    x = 10 * x + ch - '0';
    ch = getchar();
  }
  return x * t;
}
int a[N], b[N], pa[N], push_back[N];
int vis[N];
int main() {
  int T = read();
  while (T--) {
    int n = read(), m = read();
    for (int i = 1; i <= n; i++) a[i] = read(), pa[a[i]] = i;
    for (int i = 1; i <= m; i++) b[i] = read(), push_back[b[i]] = i;
    for (int i = 1; i <= m; i++) vis[b[i]] = 1;
    long long ans = 1;
    for (int i = 1; i <= m; i++) {
      int cnt = 0;
      int p = pa[b[i]];
      if (p > 1 && !vis[a[p - 1]]) cnt++;
      if (p < n && !vis[a[p + 1]]) cnt++;
      vis[b[i]] = 0;
      ans = ans * cnt % mod;
    }
    printf("%lld\n", ans);
  }
  return 0;
}
