#include <bits/stdc++.h>
using namespace std;
const int mod = 998244353;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int T;
  cin >> T;
  for (int cass = 1; cass <= T; ++cass) {
    int n, k;
    cin >> n >> k;
    vector<int> pos(n + 1);
    set<pair<int, int> > s;
    for (int i = 0; i < n; ++i) {
      int x;
      cin >> x;
      pos[x] = i;
      s.insert({i, x});
    }
    set<int> s2;
    vector<int> b(k);
    long long ans = 1;
    for (int i = 0; i < k; ++i) {
      int x;
      cin >> x;
      b[i] = x;
      s2.insert(x);
    }
    for (int i = 0; i < k; ++i) {
      int p = pos[b[i]];
      auto it = s.lower_bound({p, 0});
      int cnt = 0, cnt2 = 0, x = -1, y = -1;
      auto it2 = it;
      it2++;
      if (it2 != s.end()) {
        ++cnt;
        it++;
        x = (*it).second;
        it--;
      }
      if (it != s.begin()) {
        ++cnt;
        it--;
        y = (*it).second;
        it++;
      }
      if (cnt == 2) {
        int cnt2 = 0;
        if (s2.count(x)) {
          ++cnt2;
        }
        if (s2.count(y)) {
          ++cnt2;
        }
        if (cnt2 == 2) {
          ans = 0;
        } else if (cnt2 == 1) {
          if (s2.count(x)) {
            it--;
            s.erase(it);
          }
          if (s2.count(y)) {
            it++;
            s.erase(it);
          }
        } else {
          it--;
          s.erase(it);
          ans <<= 1;
          ans %= mod;
        }
      } else if (cnt == 1) {
        int cnt2 = 0;
        if (s2.count(x)) {
          ++cnt2;
        }
        if (s2.count(y)) {
          ++cnt2;
        }
        if (cnt2 == 1) {
          ans = 0;
        } else {
          if (x != -1) {
            it++;
            s.erase(it);
          }
          if (y != -1) {
            it--;
            s.erase(it);
          }
        }
      } else {
        ans = 0;
      }
      s2.erase(b[i]);
    }
    cout << ans << '\n';
  }
  return 0;
}
