#include <bits/stdc++.h>
using namespace std;
int T, n, m;
const int MOD = 998244353, N = 2e5 + 9;
long long a[N], b[N];
vector<pair<int, int> > vec;
unordered_map<int, int> getpos;
int main() {
  cin >> T;
  while (T--) {
    unordered_map<int, bool> mp;
    vec.clear();
    mp.clear();
    getpos.clear();
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++) {
      scanf("%lld", &a[i]);
      getpos[a[i]] = i;
    }
    for (int i = 1; i <= m; i++) {
      scanf("%lld", &b[i]);
      mp[b[i]] = true;
    }
    long long ans = 1;
    for (int i = 1; i <= m; i++) {
      int pos = getpos[b[i]];
      long long k = 0;
      if (!mp[a[pos - 1]] && pos - 1 >= 1) k++;
      if (!mp[a[pos + 1]] && pos + 1 <= n) k++;
      ans = ans * k % MOD;
      mp[b[i]] = false;
    }
    printf("%lld\n", ans);
  }
  return 0;
}
