#include <bits/stdc++.h>
using namespace std;
const double pi = 3.141592653;
const long long infi = 1000000001;
const long long mod = 998244353;
const string no = "NO\n", yes = "YES\n";
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  cin >> t;
  long long n, m, i, length, d, bank, s, k, j, x, y, ans, q;
  while (t--) {
    ans = 0;
    cin >> n >> k;
    int a[n], p[n + 1];
    for (i = 0; i < n; i++) {
      cin >> a[i];
      p[i + 1] = 0;
    }
    for (i = 0; i < k; i++) {
      cin >> x;
      p[x] = i + 1;
    }
    ans = 1;
    q = 0;
    for (i = 0; i < n; i++) {
      x = p[a[i]];
      y = 0;
      if (x) {
        if (i == 0) {
          if (p[a[i + 1]] < x) y++;
        } else if (i == n - 1) {
          if (p[a[i - 1]] < x) y++;
        } else {
          if (p[a[i + 1]] < x) y++;
          if (p[a[i - 1]] < x) y++;
        }
        ans *= y;
        ans %= mod;
        q++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
