#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    long long n, k;
    cin >> n >> k;
    vector<vector<long long>> a(n, vector<long long>(3));
    vector<long long> b(k);
    vector<int> make_pair(n + 1);
    for (int i = 0; i < n; ++i) {
      cin >> a[i][0];
      make_pair[a[i][0]] = i;
      a[i][1] = i - 1;
      a[i][2] = i < n - 1 ? i + 1 : -1;
    }
    vector<int> st(n + 1, false);
    for (auto &i : b) {
      cin >> i;
      st[i] = true;
    }
    long long ans = 1;
    for (long long i = 0; i < k; ++i) {
      int pos = make_pair[b[i]];
      int cnt = 0;
      if (a[pos][1] != -1) {
        int nbr = a[pos][1];
        if (!st[a[nbr][0]]) cnt++;
      }
      if (a[pos][2] != -1) {
        int nbr = a[pos][2];
        if (!st[a[nbr][0]]) cnt++;
      }
      st[b[i]] = false;
      int l = a[pos][1];
      int r = a[pos][2];
      if (l != -1) a[l][2] = r;
      if (r != -1) a[r][1] = l;
      if (!cnt) {
        ans = 0;
        break;
      }
      ans = (ans * cnt) % ((long long)(998244353));
    }
    cout << ans << endl;
  }
  return 0;
}
