#include <bits/stdc++.h>
using namespace std;
long long mod = 998244353;
int32_t main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n, k, i, j;
    cin >> n >> k;
    long long arr[n], final[k], count = 0;
    map<long long, long long> mp;
    set<pair<long long, long long>> s;
    set<long long> finals;
    for (i = 0; i < n; i++) {
      cin >> arr[i];
      mp[arr[i]] = count;
      s.insert({count, arr[i]});
      count++;
    }
    s.insert({-1, arr[1]});
    s.insert({count, arr[n - 2]});
    for (i = 0; i < k; i++) {
      cin >> final[i];
      finals.insert(final[i]);
    }
    long long ans = 1, flag = 0;
    for (i = 0; i < k; i++) {
      long long search = mp[final[i]];
      long long start = 0, end = n - i - 1;
      auto ptr = s.lower_bound({search, final[i]});
      auto ptr2 = ptr, ptr3 = ptr;
      auto back = --ptr2;
      auto front = ++ptr3;
      long long one = (*back).first, two = (*front).first;
      long long num = 0;
      if (one != -1 && finals.find((*back).second) == finals.end()) {
        num++;
        s.erase(back);
      }
      if (two != n && finals.find((*front).second) == finals.end()) {
        num++;
        if (num == 0) s.erase(front);
      }
      if (num == 0) {
        flag = 1;
        break;
      } else if (num == 2) {
        ans *= 2;
        ans %= mod;
      }
      if (num != 0) finals.erase(finals.find(final[i]));
    }
    if (flag == 1)
      cout << "0\n";
    else
      cout << ans << "\n";
  }
}
