#include <bits/stdc++.h>
using namespace std;
priority_queue<long long, vector<long long>, greater<long long>> pq1, pq2;
long long power(long long a, long long b) {
  long long res = 1;
  while (b > 0) {
    res *= a;
    b--;
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long t, x, b[200005], n, k, mod = 998244353, req[200005] = {0},
                                   a[200005], store[200005] = {0};
  cin >> t;
  while (t--) {
    cin >> n >> k;
    for (int i = 0; i < n; ++i) {
      cin >> a[i];
      store[a[i]] = i + 1;
    }
    for (int i = 0; i < k; ++i) {
      cin >> b[i];
      req[b[i]]++;
    }
    long long fl = 0, ans = 1;
    for (int i = 0; i < k; ++i) {
      if (store[b[i]] == 0) {
        fl = 1;
        break;
      } else {
        if (store[b[i]] == 1) {
          if (req[a[1]] != 0) {
            fl = 1;
            break;
          }
        } else if (store[b[i]] == n) {
          if (req[a[n - 2]] != 0) {
            fl = 1;
            break;
          }
        } else {
          if (req[a[store[b[i]] - 2]] == 0 && req[a[store[b[i]]]] == 0) {
            ans = (ans * 2) % mod;
          } else if (req[a[store[b[i]] - 2]] != 0 && req[a[store[b[i]]]] != 0) {
            fl = 1;
            break;
          }
        }
      }
      req[b[i]] = 0;
    }
    if (fl == 0)
      cout << ans << endl;
    else
      cout << "0" << endl;
    for (int i = 0; i < n + 3; ++i) {
      req[i] = 0;
      store[i] = 0;
    }
  }
  return 0;
}
