#include <bits/stdc++.h>
using namespace std;
long long int MOD = 1000000007;
long long int MOD1 = 998244353;
mt19937 tw(chrono::high_resolution_clock::now().time_since_epoch().count());
struct node {
  int data, next, prev;
};
void solve() {
  int n, k, res = 1;
  cin >> n >> k;
  int b[k];
  node a[n];
  set<int> s;
  unordered_map<int, int> m;
  for (int i = 0; i < n; i++) {
    cin >> a[i].data;
    a[i].next = i + 1;
    a[i].prev = i - 1;
    m[a[i].data] = i;
  }
  for (int i = 0; i < k; i++) {
    cin >> b[i];
    s.insert(b[i]);
  }
  for (int i = 0; i < k; i++) {
    int p = b[i];
    int j = m[p];
    if (a[j].next < n && a[j].prev >= 0) {
      int t1 = a[a[j].next].data;
      int t2 = a[a[j].prev].data;
      if (s.find(t1) == s.end() && s.find(t2) == s.end()) {
        int k = a[j].next;
        a[j].next = a[k].next;
        if (k < n) {
          a[k].prev = j;
        }
        res = (res * 2) % MOD1;
      } else if (s.find(t1) == s.end()) {
        int k = a[j].next;
        a[j].next = a[k].next;
        if (k < n) {
          a[k].prev = j;
        }
      } else if (s.find(t2) == s.end()) {
        int k = a[j].prev;
        a[j].prev = a[k].prev;
        if (k >= 0) {
          a[k].prev = j;
        }
      } else {
        cout << 0 << "\n";
        return;
      }
    } else if (a[j].next < n) {
      int t = a[a[j].next].data;
      if (s.find(t) == s.end()) {
        int k = a[j].next;
        a[j].next = a[k].next;
        if (k < n) {
          a[k].prev = j;
        }
      } else {
        cout << 0 << "\n";
        return;
      }
    } else if (a[j].prev >= 0) {
      int t = a[a[j].prev].data;
      if (s.find(t) == s.end()) {
        int k = a[j].prev;
        a[j].prev = a[k].prev;
        if (k >= 0) {
          a[k].prev = j;
        }
      } else {
        cout << 0 << "\n";
        return;
      }
    }
    s.erase(b[i]);
  }
  cout << res << "\n";
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int t;
  cin >> t;
  while (t--) solve();
}
