#include <bits/stdc++.h>
using namespace std;
const int N = 1e7 + 10;
const int mod = 998244353;
map<int, int> m;
set<int> s;
int a[N], b[N];
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, k, sum = 0;
    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
      cin >> a[i];
      m[a[i]] = i;
    }
    for (int i = 1; i <= k; i++) {
      cin >> b[i];
      s.insert(b[i]);
    }
    for (int i = 1; i <= k; i++) {
      int e = 0, p = m[b[i]];
      if (p != 1 && s.count(a[p - 1]) == 0) e++;
      if (p != n && s.count(a[p + 1]) == 0) e++;
      if (e > 0)
        s.erase(b[i]);
      else {
        sum = -1;
        break;
      }
      if (e == 2) sum++;
    }
    if (sum == -1) {
      cout << "0" << endl;
    } else {
      int ans = 1;
      for (int i = 0; i < sum; i++) {
        ans = ans << 1;
        ans %= mod;
      }
      cout << ans << endl;
    }
    m.clear();
    s.clear();
  }
  return 0;
}
