#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fll;
const mt19937 rng((int)chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 998244353;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    vector<int> a(n);
    for (int &i : a) cin >> i;
    map<int, int> pos;
    for (int i = 0; i < n; i++) pos[a[i]] = i;
    vector<int> b(k);
    for (int &i : b) cin >> i;
    long long ans = 1;
    set<int> sb(b.begin(), b.end());
    for (int i : b) {
      int p = pos[i];
      long long nxt = 0;
      if (p != 0) {
        int vl = a[p - 1];
        if (sb.count(vl) == 0) nxt += ans;
      }
      if (p != n - 1) {
        int vr = a[p + 1];
        if (sb.count(vr) == 0) nxt += ans;
      }
      sb.erase(i);
      if (nxt >= MOD) nxt -= MOD;
      ans = nxt;
    }
    cout << ans << '\n';
  }
  exit(0);
}
