#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
int32_t main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n, k;
    cin >> n >> k;
    long long a[n], b[k];
    unordered_set<long long> s;
    map<long long, long long> idx;
    for (long long i = 0; i < n; i++) cin >> a[i];
    for (long long i = 0; i < k; i++) cin >> b[i];
    for (long long i = 0; i < n; i++) {
      idx[a[i]] = i;
    }
    for (long long i = 0; i < k; i++) {
      s.insert(b[i]);
    }
    long long ans = 1;
    long long ok = 0;
    for (long long i = 0; i < k; i++) {
      if (!idx.count(b[i])) {
        ok = 1;
        break;
      } else {
        long long id = idx[b[i]];
        long long mul = 0;
        if (id - 1 >= 0 and !s.count(a[id - 1])) mul++;
        if (id + 1 < n and !s.count(a[id + 1])) mul++;
        ans = (ans * mul) % mod;
        s.erase(a[id]);
      }
    }
    if (ok)
      cout << 0 << endl;
    else
      cout << ans << endl;
  }
}
