#include <bits/stdc++.h>
using namespace std;
const int N = 200100;
int T, n, m, a[N], b[N], ans, pos[N], mod = 998244353;
set<pair<int, int> > s;
set<int> after;
set<pair<int, int> >::iterator it;
void _clear() {
  s.clear();
  ans = 1;
}
pair<int, int> getNext(int i) {
  it = s.upper_bound(make_pair(i, 1e9));
  if (it == s.end()) return {-1, -1};
  return *it;
}
pair<int, int> getBefore(int i) {
  it = s.lower_bound(make_pair(i, 0));
  if (it == s.begin()) return {-1, -1};
  it--;
  return *it;
}
int main() {
  scanf("%d", &T);
  while (T--) {
    _clear();
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
      s.insert({i, a[i]});
      pos[a[i]] = i;
    }
    for (int i = 1; i <= m; i++) {
      scanf("%d", &b[i]);
      after.insert(b[i]);
    }
    for (int i = 1; i <= m; i++) {
      after.erase(b[i]);
      pair<int, int> pos1 = getNext(pos[b[i]]);
      pair<int, int> pos2 = getBefore(pos[b[i]]);
      int p = 0;
      if (pos1.first != -1 && after.find(pos1.second) == after.end()) p++;
      if (pos2.first != -1 && after.find(pos2.second) == after.end()) p++;
      ans = ans * p % mod;
      s.erase({pos[b[i]], b[i]});
    }
    printf("%d\n", ans);
  }
}
