#include <bits/stdc++.h>
using namespace std;
long long const mod = 998244353;
int a[300005], cnt[300005], n, t, k;
queue<int> q;
map<int, int> m;
bool v[300005];
int main(void) {
  while (scanf("%d", &t) != EOF) {
    while (t--) {
      scanf("%d %d", &n, &k);
      memset(v, 0, sizeof(v));
      for (int i = 1; i <= n; i++) {
        scanf("%d", &a[i]);
        m[a[i]] = i;
      }
      v[0] = v[n + 1] = 1;
      int x;
      long long tmp;
      for (int i = 1; i <= k; i++) {
        scanf("%d", &x);
        q.push(x);
        v[m[x]] = 1;
      }
      long long ans = 1;
      while (!q.empty()) {
        x = m[q.front()];
        q.pop();
        tmp = 2 - v[x - 1] - v[x + 1];
        v[x] = 0;
        ans = ans * tmp % mod;
      }
      printf("%lld\n", ans);
    }
  }
  return 0;
}
