#include <bits/stdc++.h>
using namespace std;
long long mod1 = (998244353);
long long mod = 1000000007;
int main() {
  long long t;
  cin >> t;
  while (t--) {
    long long n, k;
    cin >> n >> k;
    long long a[n];
    vector<long long> p(n, 0);
    map<long long, long long> mp;
    for (int j = 0; j < n; j++) {
      cin >> a[j];
      mp[a[j]] = j;
    }
    long long b[k];
    long long an = 1;
    for (int j = 0; j < k; j++) {
      cin >> b[j];
      if (mp.find(b[j]) == mp.end()) {
        an = 0;
      } else {
        p[mp[b[j]]] = 1;
      }
    }
    if (an == 0) {
      cout << 0 << endl;
      continue;
    }
    for (int j = 0; j < k; j++) {
      long long i = mp[b[j]];
      long long k = 0;
      if (i < n - 1 && p[i + 1] == 0) {
        k++;
      }
      if (i > 0 && p[i - 1] == 0) {
        k++;
      }
      if (k == 0) {
        an = 0;
        break;
      } else {
        an *= k;
        p[i] = 0;
      }
      an %= mod1;
    }
    cout << an << endl;
  }
}
