#include <bits/stdc++.h>
using namespace std;
set<int> s;
int a[200005];
int b[200005], u[200005];
int n, k;
int main() {
  int T;
  scanf("%d", &T);
  for (; T--;) {
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for (int i = 1; i <= k; i++) scanf("%d", &b[i]);
    for (int i = 1; i <= n; i++) u[a[i]] = i;
    s.clear();
    for (int i = 1; i <= k; i++) s.insert(b[i]);
    long long ans = 1;
    for (int i = 1; i <= k; i++) {
      int x = u[b[i]];
      int now = 0;
      int y;
      if (x > 1) {
        y = x - 1;
        if (s.find(a[y]) == s.end()) now++;
      }
      if (x < n) {
        y = x + 1;
        if (s.find(a[y]) == s.end()) now++;
      }
      ans = ans * now % 998244353;
      s.erase(b[i]);
    }
    printf("%lld\n", ans);
  }
}
