#include <bits/stdc++.h>
using namespace std;
inline int rd() {
  int x = 0, flg = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') flg = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + (ch ^ 48);
    ch = getchar();
  }
  return x * flg;
}
struct node {
  int l, r;
} s[200010];
int n, m, t, l, r, x, tag[200010], vis[200010], a[200010], b[200010];
int main() {
  t = rd();
  while (t--) {
    int ans = 1;
    n = rd(), m = rd();
    for (register int i = 1; i <= n; ++i) a[i] = rd(), vis[a[i]] = i;
    for (register int i = 1; i <= m; ++i) b[i] = rd(), tag[vis[b[i]]] = 1;
    for (register int i = 1; i <= n; ++i) s[i].l = i - 1, s[i].r = i + 1;
    for (register int i = 1; i <= m; ++i) {
      int pos = vis[b[i]];
      int cnt = 0;
      if (s[pos].r != n + 1 && !tag[s[pos].r]) cnt += 1;
      if (s[pos].l != 0 && !tag[s[pos].l]) cnt += 10;
      if (cnt == 0) {
        ans = 0;
        break;
      } else if (cnt < 11) {
        tag[pos] = 0;
        if (cnt == 1)
          s[s[s[pos].r].r].l = pos, s[pos].r = s[s[pos].r].r;
        else
          s[s[s[pos].l].l].r = pos, s[pos].l = s[s[pos].l].l;
      } else {
        tag[pos] = 0;
        s[s[s[pos].r].r].l = pos, s[pos].r = s[s[pos].r].r;
        ans += ans;
        if (ans >= 998244353) ans -= 998244353;
      }
    }
    cout << ans << endl;
    for (register int i = 1; i <= n; ++i) s[i].l = s[i].r = tag[i] = vis[i] = 0;
  }
  return 0;
}
