#include <bits/stdc++.h>
using namespace std;
int indexOf[200001];
int order[200001];
void init(int n, int k, int a[], int b[]) {
  for (int i = 1; i <= n; i++) {
    indexOf[a[i]] = i;
    order[i] = 0;
  }
  for (int i = 1; i <= k; i++) {
    order[indexOf[b[i]]] = i;
  }
}
int solve(int n, int k, int a[], int b[]) {
  init(n, k, a, b);
  int totalCases = 1;
  for (int i = 1; i <= k; i++) {
    int cur = indexOf[b[i]];
    bool leftIsSelectable = (cur > 1 && order[cur] > order[cur - 1]);
    bool rightIsSelectable = (cur < n && order[cur] > order[cur + 1]);
    int cases = leftIsSelectable + rightIsSelectable;
    totalCases = (totalCases * cases) % 998244353;
  }
  return totalCases;
}
int main(int argc, char** argv) {
  static int a[200001];
  static int b[200001];
  int T;
  cin >> T;
  for (int tc = 1; tc <= T; ++tc) {
    int n, k;
    cin >> n >> k;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= k; i++) cin >> b[i];
    int answer = solve(n, k, a, b);
    cout << answer << endl;
  }
  return 0;
}
