#include <bits/stdc++.h>
using namespace std;
char s[100001];
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    map<long long, long long> map1, map2;
    long long n, k, res = 1;
    cin >> n >> k;
    long long arr[n], arr1[k];
    for (long long i = 0; i < n; i++) {
      cin >> arr[i];
      map1[arr[i]] = i;
    }
    for (long long i = 0; i < k; i++) {
      cin >> arr1[i];
      map2[arr1[i]] = i;
    }
    for (long long i = 0; i < k; i++) {
      long long count = 0;
      auto it = map1.find(arr1[i]);
      if (it != map1.end()) {
        if (it->second > 0) {
          long long a = (it->second) - 1;
          auto itr = map2.find(arr[a]);
          if (itr == map2.end() || itr->second < i) {
            count++;
          }
        }
        if (it->second < n - 1) {
          long long a = it->second + 1;
          auto itr = map2.find(arr[a]);
          if (itr == map2.end() || itr->second < i) {
            count++;
          }
        }
      } else {
        count = 0;
      }
      res *= count;
      res = res % 998244353;
    }
    cout << res << endl;
  }
  return 0;
}
