#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long int llINF = 0x3f3f3f3f3f3f3f;
const int MOD = 998244353;
int n, k;
int a[200200], b[200200], pre[200200], pos[200200];
int mapa[200200];
int fut[200200];
void init() {
  for (int i = 1; i <= n; i++) {
    pre[i] = i - 1;
    pos[i] = i + 1;
    mapa[i] = 0;
    fut[i] = 0;
  }
}
void del(int i) {
  pos[pre[i]] = pos[i];
  pre[pos[i]] = pre[i];
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int T;
  cin >> T;
  while (T--) {
    cin >> n >> k;
    init();
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= n; i++) mapa[a[i]] = i;
    for (int i = 1; i <= k; i++) cin >> b[i];
    for (int i = 1; i <= k; i++) fut[b[i]]++;
    long long int ans = 1;
    for (int i = 1; i <= k; i++) {
      long long int aux = 0;
      int j = mapa[b[i]];
      if (pre[j] != 0 && fut[a[pre[j]]] == 0) {
        del(pre[j]);
        aux++;
      }
      if (pos[j] <= n && fut[a[pos[j]]] == 0) {
        aux++;
        if (aux == 1) del(pos[j]);
      }
      ans *= aux;
      ans %= MOD;
      fut[b[i]]--;
    }
    cout << ans << endl;
  }
}
