#include <bits/stdc++.h>

//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
//using namespace __gnu_pbds;

const double pi = 3.141592653589793;

#define xx          first
#define yy          second
#define mp          make_pair
#define intl        long long
#define filein      freopen("input.txt", "r", stdin)
#define fileout     freopen("output.txt", "w", stdout)
#define debug       printf("yes\n")
#define val_of(x)   cout << #x << " is " << x << "   "
#define what_is(x)  cout << #x << " is " << x << endl
#define pb          push_back
#define eb          emplace_back
#define pii         pair<int, int>
#define piii        pair<pii, int>

//typedef tree < intl , null_type ,less<intl>,rb_tree_tag,tree_order_statistics_node_update > ordered_set;

intl ara[1005][1005];
priority_queue<intl>pr, pc;
intl rr[1000005], cc[1000005];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    //filein;
    intl n, m, k, p;
    cin >> n >> m >> k >> p;
    intl row = 0, col = 0;
    for( int i = 0 ; i < n; i++ )
    {
        for( int j = 0; j < m; j++ )
        {
            cin >> ara[i][j];
        }
    }
    for( int i = 0 ; i < n; i++ )
    {
        intl sum = 0;
        for( int j = 0; j < m; j++ )
        {
            sum += ara[i][j];
        }
        pr.push(sum);
    }
    for( int i = 0 ; i < m; i++ )
    {
        intl sum = 0;
        for( int j = 0; j < n; j++ )
        {
            sum += ara[j][i];
        }
        pc.push(sum);
    }

    for( int i = 1; i <= k; i++ )
    {
        intl d;
        d = pr.top();
        pr.pop();
        rr[i] = d;
        d -= m*p;
        pr.push(d);

        d = pc.top();
        pc.pop();
        cc[i] = d;
        d -= n*p;
        pc.push(d);
        rr[i] += rr[i-1];
        cc[i] += cc[i-1];
    }
    intl ans = -1e18;

    for( intl i = 0; i <= k; i++ )
    {
        ans= max( ans, rr[i] + cc[k-i] - (k-i)*i*p );
    }
    cout << ans << endl;
    return 0;
}


#include <bits/stdc++.h>

//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
//using namespace __gnu_pbds;

const double pi = 3.141592653589793;

#define xx          first
#define yy          second
#define mp          make_pair
#define intl        long long
#define filein      freopen("input.txt", "r", stdin)
#define fileout     freopen("output.txt", "w", stdout)
#define debug       printf("yes\n")
#define val_of(x)   cout << #x << " is " << x << "   "
#define what_is(x)  cout << #x << " is " << x << endl
#define pb          push_back
#define eb          emplace_back
#define pii         pair<int, int>
#define piii        pair<pii, int>

//typedef tree < intl , null_type ,less<intl>,rb_tree_tag,tree_order_statistics_node_update > ordered_set;

intl ara[1005][1005];
priority_queue<intl>pr, pc;
intl rr[1000005], cc[1000005];

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    //filein;
    intl n, m, k, p;
    cin >> n >> m >> k >> p;
    intl row = 0, col = 0;
    for( int i = 0 ; i < n; i++ )
    {
        for( int j = 0; j < m; j++ )
        {
            cin >> ara[i][j];
        }
    }
    for( int i = 0 ; i < n; i++ )
    {
        intl sum = 0;
        for( int j = 0; j < m; j++ )
        {
            sum += ara[i][j];
        }
        pr.push(sum);
    }
    for( int i = 0 ; i < m; i++ )
    {
        intl sum = 0;
        for( int j = 0; j < n; j++ )
        {
            sum += ara[j][i];
        }
        pc.push(sum);
    }

    for( int i = 1; i <= k; i++ )
    {
        intl d;
        d = pr.top();
        pr.pop();
        rr[i] = d;
        d -= m*p;
        pr.push(d);

        d = pc.top();
        pc.pop();
        cc[i] = d;
        d -= n*p;
        pc.push(d);
        rr[i] += rr[i-1];
        cc[i] += cc[i-1];

    }
    intl ans = 0;
    for( int i = 0; i <= k; i++ )
    {
        ans= max( ans, rr[k-i] + cc[i] - (k-i)*i*p );
    }
    cout << ans << endl;
    return 0;
}


/*
Rathin Bhargava
IIIT Bangalore

*/
#include<bits/stdc++.h>
#define mt make_tuple
#define mp make_pair
#define pu push_back
#define INF 1000000001
#define MOD 1000000007
#define ll long long int
#define ld long double
#define vi vector<int>
#define vll vector<long long int>
#define fi first
#define se second
#define pr(v) { for(int i=0;i<v.size();i++) { v[i]==INF? cout<<"INF " : cout<<v[i]<<" "; } cout<<endl;}
#define t1(x)                cerr<<#x<<" : "<<x<<endl
#define t2(x, y)             cerr<<#x<<" : "<<x<<" "<<#y<<" : "<<y<<endl
#define t3(x, y, z)          cerr<<#x<<" : " <<x<<" "<<#y<<" : "<<y<<" "<<#z<<" : "<<z<<endl
#define t4(a,b,c,d)          cerr<<#a<<" : "<<a<<" "<<#b<<" : "<<b<<" "<<#c<<" : "<<c<<" "<<#d<<" : "<<d<<endl
#define t5(a,b,c,d,e)          cerr<<#a<<" : "<<a<<" "<<#b<<" : "<<b<<" "<<#c<<" : "<<c<<" "<<#d<<" : "<<d<<" "<<#e<<" : "<<e<<endl
#define t6(a,b,c,d,e,f)          cerr<<#a<<" : "<<a<<" "<<#b<<" : "<<b<<" "<<#c<<" : "<<c<<" "<<#d<<" : "<<d<<" "<<#e<<" : "<<e<<" "<<#f<<" : "<<f<<endl
#define GET_MACRO(_1,_2,_3,_4,_5,_6,NAME,...) NAME
#define t(...) GET_MACRO(__VA_ARGS__,t6,t5, t4, t3, t2, t1)(__VA_ARGS__)
#define _ cerr<<"here"<<endl;
#define __ {ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);}

using namespace std;

int main()
{
  __;
  int n,m,k,p;
  cin>>n>>m>>k>>p;
  vector<vector<int> > v(n,vector<int>(m));
  for(int i=0;i<n;i++) for(int j=0;j<m;j++) cin>>v[i][j];
  priority_queue<int> a,b;

  for(int i=0;i<n;i++)
  {
    int sum1 = 0;
    for(int j=0;j<m;j++) sum1+=v[i][j];
    a.push(sum1);
  }

  for(int j=0;j<m;j++)
  {
    int sum1 = 0;
    for(int i=0;i<n;i++) sum1+=v[i][j];
    b.push(sum1);
  }

  int subc = 0,subd = 0;
  int count = 0;

  while(k--)
  {
    int c = a.top(), d = b.top();
    c-=subc;
    d-=subd;
    if(c>d)
    {
      count+=c;
      a.pop();
      c-=p*m;
      subd+=p;
      a.push(c);
    }
    else
    {
      count+=d;
      b.pop();
      d-=p*m;
      subc+=p;
      b.push(d);
    }
  }

  cout<<count<<endl;
  return 0;
}


/*
Rathin Bhargava
IIIT Bangalore

*/
#include<bits/stdc++.h>
#define mt make_tuple
#define mp make_pair
#define pu push_back
#define INF 1000000001
#define MOD 1000000007
#define ll long long int
#define ld long double
#define vi vector<int>
#define vll vector<long long int>
#define fi first
#define se second
#define pr(v) { for(int i=0;i<v.size();i++) { v[i]==INF? cout<<"INF " : cout<<v[i]<<" "; } cout<<endl;}
#define t1(x)                cerr<<#x<<" : "<<x<<endl
#define t2(x, y)             cerr<<#x<<" : "<<x<<" "<<#y<<" : "<<y<<endl
#define t3(x, y, z)          cerr<<#x<<" : " <<x<<" "<<#y<<" : "<<y<<" "<<#z<<" : "<<z<<endl
#define t4(a,b,c,d)          cerr<<#a<<" : "<<a<<" "<<#b<<" : "<<b<<" "<<#c<<" : "<<c<<" "<<#d<<" : "<<d<<endl
#define t5(a,b,c,d,e)          cerr<<#a<<" : "<<a<<" "<<#b<<" : "<<b<<" "<<#c<<" : "<<c<<" "<<#d<<" : "<<d<<" "<<#e<<" : "<<e<<endl
#define t6(a,b,c,d,e,f)          cerr<<#a<<" : "<<a<<" "<<#b<<" : "<<b<<" "<<#c<<" : "<<c<<" "<<#d<<" : "<<d<<" "<<#e<<" : "<<e<<" "<<#f<<" : "<<f<<endl
#define GET_MACRO(_1,_2,_3,_4,_5,_6,NAME,...) NAME
#define t(...) GET_MACRO(__VA_ARGS__,t6,t5, t4, t3, t2, t1)(__VA_ARGS__)
#define _ cerr<<"here"<<endl;
#define __ {ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);}

using namespace std;

int main()
{
  __;
  int n,m,k,p;
  cin>>n>>m>>k>>p;
  vector<vector<int> > v(n,vector<int>(m));
  for(int i=0;i<n;i++) for(int j=0;j<m;j++) cin>>v[i][j];
  priority_queue<ll> a,b;

  for(int i=0;i<n;i++)
  {
    ll sum1 = 0;
    for(int j=0;j<m;j++) sum1+=v[i][j];
    a.push(sum1);
  }

  for(int j=0;j<m;j++)
  {
    ll sum1 = 0;
    for(int i=0;i<n;i++) sum1+=v[i][j];
    b.push(sum1);
  }

  ll subc = 0,subd = 0;
  ll count = 0;

  while(k--)
  {
    ll c = a.top(), d = b.top();
    c-=subc;
    d-=subd;
    if(c>d)
    {
      count+=c;
      a.pop();
      c-=p*m;
      subd+=p;
      a.push(c);
    }
    else
    {
      count+=d;
      b.pop();
      d-=p*m;
      subc+=p;
      b.push(d);
    }
  }

  cout<<count<<endl;
  return 0;
}


/*
Rathin Bhargava
IIIT Bangalore

*/
#include<bits/stdc++.h>
#define mt make_tuple
#define mp make_pair
#define pu push_back
#define INF 1e18
#define MOD 1000000007
#define ll long long int
#define ld long double
#define vi vector<int>
#define vll vector<long long int>
#define fi first
#define se second
#define pr(v) { for(int i=0;i<v.size();i++) { v[i]==INF? cout<<"INF " : cout<<v[i]<<" "; } cout<<endl;}
#define t1(x)                cerr<<#x<<" : "<<x<<endl
#define t2(x, y)             cerr<<#x<<" : "<<x<<" "<<#y<<" : "<<y<<endl
#define t3(x, y, z)          cerr<<#x<<" : " <<x<<" "<<#y<<" : "<<y<<" "<<#z<<" : "<<z<<endl
#define t4(a,b,c,d)          cerr<<#a<<" : "<<a<<" "<<#b<<" : "<<b<<" "<<#c<<" : "<<c<<" "<<#d<<" : "<<d<<endl
#define t5(a,b,c,d,e)          cerr<<#a<<" : "<<a<<" "<<#b<<" : "<<b<<" "<<#c<<" : "<<c<<" "<<#d<<" : "<<d<<" "<<#e<<" : "<<e<<endl
#define t6(a,b,c,d,e,f)          cerr<<#a<<" : "<<a<<" "<<#b<<" : "<<b<<" "<<#c<<" : "<<c<<" "<<#d<<" : "<<d<<" "<<#e<<" : "<<e<<" "<<#f<<" : "<<f<<endl
#define GET_MACRO(_1,_2,_3,_4,_5,_6,NAME,...) NAME
#define t(...) GET_MACRO(__VA_ARGS__,t6,t5, t4, t3, t2, t1)(__VA_ARGS__)
#define _ cerr<<"here"<<endl;
#define __ {ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);}

using namespace std;

ll max(ll x, ll y)
{
  if(x>y) return x;
  return y;
}

int main()
{
  __;
  int n,m;
  ll k,p;
  cin>>n>>m>>k>>p;
  vector<vector<int> > v(n,vector<int>(m));
  for(int i=0;i<n;i++) for(int j=0;j<m;j++) cin>>v[i][j];
  priority_queue<ll> a,b;

  for(int i=0;i<n;i++)
  {
    ll sum1 = 0;
    for(int j=0;j<m;j++) sum1+=v[i][j];
    a.push(sum1);
  }

  for(int j=0;j<m;j++)
  {
    ll sum1 = 0;
    for(int i=0;i<n;i++) sum1+=v[i][j];
    b.push(sum1);
  }

  ll subc = 0,subd = 0;
  ll count = 0;

  vector<ll> x(k+1), y(k+1);
  ll sum1 = 0, sum2 = 0;
  for(int i=1;i<=k;i++)
  {
    ll c = a.top();
    sum1+=c;
    c-=p*m;
    a.pop();
    a.push(c);
    x[i] = sum1;
  }

  for(int i=1;i<=k;i++)
  {
    ll d = b.top();
    b.pop();
    sum2+=d;
    y[i] = sum2;
    d-=p*n;
    b.push(d);
  }

  ll max1 = -INF;
  for(int i=0;i<=k;i++)
  {
    ll c = x[i], d = y[k-i];
    max1 = max(max1,c+d-(p*(k-i)*i));
  }

  cout<<max1<<endl;
  return 0;
}


/*
Rathin Bhargava
IIIT Bangalore

*/
#include<bits/stdc++.h>
#define mt make_tuple
#define mp make_pair
#define pu push_back
#define INF 1000000001
#define MOD 1000000007
#define ll long long int
#define ld long double
#define vi vector<int>
#define vll vector<long long int>
#define fi first
#define se second
#define pr(v) { for(int i=0;i<v.size();i++) { v[i]==INF? cout<<"INF " : cout<<v[i]<<" "; } cout<<endl;}
#define t1(x)                cerr<<#x<<" : "<<x<<endl
#define t2(x, y)             cerr<<#x<<" : "<<x<<" "<<#y<<" : "<<y<<endl
#define t3(x, y, z)          cerr<<#x<<" : " <<x<<" "<<#y<<" : "<<y<<" "<<#z<<" : "<<z<<endl
#define t4(a,b,c,d)          cerr<<#a<<" : "<<a<<" "<<#b<<" : "<<b<<" "<<#c<<" : "<<c<<" "<<#d<<" : "<<d<<endl
#define t5(a,b,c,d,e)          cerr<<#a<<" : "<<a<<" "<<#b<<" : "<<b<<" "<<#c<<" : "<<c<<" "<<#d<<" : "<<d<<" "<<#e<<" : "<<e<<endl
#define t6(a,b,c,d,e,f)          cerr<<#a<<" : "<<a<<" "<<#b<<" : "<<b<<" "<<#c<<" : "<<c<<" "<<#d<<" : "<<d<<" "<<#e<<" : "<<e<<" "<<#f<<" : "<<f<<endl
#define GET_MACRO(_1,_2,_3,_4,_5,_6,NAME,...) NAME
#define t(...) GET_MACRO(__VA_ARGS__,t6,t5, t4, t3, t2, t1)(__VA_ARGS__)
#define _ cerr<<"here"<<endl;
#define __ {ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);}

using namespace std;

ll max(ll x, ll y)
{
  if(x>y) return x;
  return y;
}

int main()
{
  __;
  int n,m,k,p;
  cin>>n>>m>>k>>p;
  vector<vector<int> > v(n,vector<int>(m));
  for(int i=0;i<n;i++) for(int j=0;j<m;j++) cin>>v[i][j];
  priority_queue<ll> a,b;

  for(int i=0;i<n;i++)
  {
    ll sum1 = 0;
    for(int j=0;j<m;j++) sum1+=v[i][j];
    a.push(sum1);
  }

  for(int j=0;j<m;j++)
  {
    ll sum1 = 0;
    for(int i=0;i<n;i++) sum1+=v[i][j];
    b.push(sum1);
  }

  ll subc = 0,subd = 0;
  ll count = 0;

  vector<ll> x(k+1), y(k+1);
  ll sum1 = 0, sum2 = 0;
  for(int i=1;i<=k;i++)
  {
    ll c = a.top();
    sum1+=c;
    c-=p*m;
    a.pop();
    a.push(c);
    x[i] = sum1;
  }

  for(int i=1;i<=k;i++)
  {
    ll d = b.top();
    b.pop();
    sum2+=d;
    y[i] = sum2;
    d-=p*n;
    b.push(d);
  }

  ll max1 = -INF;
  for(int i=0;i<=k;i++)
  {
    ll c = x[i], d = y[k-i];
    max1 = max(max1,c+d-(p*(k-i)*i));
  }

  cout<<max1<<endl;
  return 0;
}


/*
Rathin Bhargava
IIIT Bangalore

*/
#include<bits/stdc++.h>
#define mt make_tuple
#define mp make_pair
#define pu push_back
#define INF 1000000001
#define MOD 1000000007
#define ll long long int
#define ld long double
#define vi vector<int>
#define vll vector<long long int>
#define fi first
#define se second
#define pr(v) { for(int i=0;i<v.size();i++) { v[i]==INF? cout<<"INF " : cout<<v[i]<<" "; } cout<<endl;}
#define t1(x)                cerr<<#x<<" : "<<x<<endl
#define t2(x, y)             cerr<<#x<<" : "<<x<<" "<<#y<<" : "<<y<<endl
#define t3(x, y, z)          cerr<<#x<<" : " <<x<<" "<<#y<<" : "<<y<<" "<<#z<<" : "<<z<<endl
#define t4(a,b,c,d)          cerr<<#a<<" : "<<a<<" "<<#b<<" : "<<b<<" "<<#c<<" : "<<c<<" "<<#d<<" : "<<d<<endl
#define t5(a,b,c,d,e)          cerr<<#a<<" : "<<a<<" "<<#b<<" : "<<b<<" "<<#c<<" : "<<c<<" "<<#d<<" : "<<d<<" "<<#e<<" : "<<e<<endl
#define t6(a,b,c,d,e,f)          cerr<<#a<<" : "<<a<<" "<<#b<<" : "<<b<<" "<<#c<<" : "<<c<<" "<<#d<<" : "<<d<<" "<<#e<<" : "<<e<<" "<<#f<<" : "<<f<<endl
#define GET_MACRO(_1,_2,_3,_4,_5,_6,NAME,...) NAME
#define t(...) GET_MACRO(__VA_ARGS__,t6,t5, t4, t3, t2, t1)(__VA_ARGS__)
#define _ cerr<<"here"<<endl;
#define __ {ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);}

using namespace std;

int main()
{
  __;
  int n,m,k,p;
  cin>>n>>m>>k>>p;
  vector<vector<int> > v(n,vector<int>(m));
  for(int i=0;i<n;i++) for(int j=0;j<m;j++) cin>>v[i][j];
  priority_queue<ll> a,b;

  for(int i=0;i<n;i++)
  {
    int sum1 = 0;
    for(int j=0;j<m;j++) sum1+=v[i][j];
    a.push(sum1);
  }

  for(int j=0;j<m;j++)
  {
    int sum1 = 0;
    for(int i=0;i<n;i++) sum1+=v[i][j];
    b.push(sum1);
  }

  ll subc = 0,subd = 0;
  ll count = 0;

  while(k--)
  {
    ll c = a.top(), d = b.top();
    c-=subc;
    d-=subd;
    if(c>d)
    {
      count+=c;
      a.pop();
      c-=p*m;
      subd+=p;
      a.push(c);
    }
    else
    {
      count+=d;
      b.pop();
      d-=p*m;
      subc+=p;
      b.push(d);
    }
  }

  cout<<count<<endl;
  return 0;
}


/*
Rathin Bhargava
IIIT Bangalore

*/
#include<bits/stdc++.h>
#define mt make_tuple
#define mp make_pair
#define pu push_back
#define INF 1e18
#define MOD 1000000007
#define ll long long int
#define ld long double
#define vi vector<int>
#define vll vector<long long int>
#define fi first
#define se second
#define pr(v) { for(int i=0;i<v.size();i++) { v[i]==INF? cout<<"INF " : cout<<v[i]<<" "; } cout<<endl;}
#define t1(x)                cerr<<#x<<" : "<<x<<endl
#define t2(x, y)             cerr<<#x<<" : "<<x<<" "<<#y<<" : "<<y<<endl
#define t3(x, y, z)          cerr<<#x<<" : " <<x<<" "<<#y<<" : "<<y<<" "<<#z<<" : "<<z<<endl
#define t4(a,b,c,d)          cerr<<#a<<" : "<<a<<" "<<#b<<" : "<<b<<" "<<#c<<" : "<<c<<" "<<#d<<" : "<<d<<endl
#define t5(a,b,c,d,e)          cerr<<#a<<" : "<<a<<" "<<#b<<" : "<<b<<" "<<#c<<" : "<<c<<" "<<#d<<" : "<<d<<" "<<#e<<" : "<<e<<endl
#define t6(a,b,c,d,e,f)          cerr<<#a<<" : "<<a<<" "<<#b<<" : "<<b<<" "<<#c<<" : "<<c<<" "<<#d<<" : "<<d<<" "<<#e<<" : "<<e<<" "<<#f<<" : "<<f<<endl
#define GET_MACRO(_1,_2,_3,_4,_5,_6,NAME,...) NAME
#define t(...) GET_MACRO(__VA_ARGS__,t6,t5, t4, t3, t2, t1)(__VA_ARGS__)
#define _ cerr<<"here"<<endl;
#define __ {ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);}

using namespace std;

ll max(ll x, ll y)
{
  if(x>y) return x;
  return y;
}

int main()
{
  __;
  int n,m,k,p;
  cin>>n>>m>>k>>p;
  vector<vector<int> > v(n,vector<int>(m));
  for(int i=0;i<n;i++) for(int j=0;j<m;j++) cin>>v[i][j];
  priority_queue<ll> a,b;

  for(int i=0;i<n;i++)
  {
    ll sum1 = 0;
    for(int j=0;j<m;j++) sum1+=v[i][j];
    a.push(sum1);
  }

  for(int j=0;j<m;j++)
  {
    ll sum1 = 0;
    for(int i=0;i<n;i++) sum1+=v[i][j];
    b.push(sum1);
  }

  ll subc = 0,subd = 0;
  ll count = 0;

  vector<ll> x(k+1), y(k+1);
  ll sum1 = 0, sum2 = 0;
  for(int i=1;i<=k;i++)
  {
    ll c = a.top();
    sum1+=c;
    c-=p*m;
    a.pop();
    a.push(c);
    x[i] = sum1;
  }

  for(int i=1;i<=k;i++)
  {
    ll d = b.top();
    b.pop();
    sum2+=d;
    y[i] = sum2;
    d-=p*n;
    b.push(d);
  }

  ll max1 = -INF;
  for(int i=0;i<=k;i++)
  {
    ll c = x[i], d = y[k-i];
    max1 = max(max1,c+d-(p*(k-i)*i));
  }

  cout<<max1<<endl;
  return 0;
}


/*
Rathin Bhargava
IIIT Bangalore

*/
#include<bits/stdc++.h>
#define mt make_tuple
#define mp make_pair
#define pu push_back
#define INF 1000000001
#define MOD 1000000007
#define ll long long int
#define ld long double
#define vi vector<int>
#define vll vector<long long int>
#define fi first
#define se second
#define pr(v) { for(int i=0;i<v.size();i++) { v[i]==INF? cout<<"INF " : cout<<v[i]<<" "; } cout<<endl;}
#define t1(x)                cerr<<#x<<" : "<<x<<endl
#define t2(x, y)             cerr<<#x<<" : "<<x<<" "<<#y<<" : "<<y<<endl
#define t3(x, y, z)          cerr<<#x<<" : " <<x<<" "<<#y<<" : "<<y<<" "<<#z<<" : "<<z<<endl
#define t4(a,b,c,d)          cerr<<#a<<" : "<<a<<" "<<#b<<" : "<<b<<" "<<#c<<" : "<<c<<" "<<#d<<" : "<<d<<endl
#define t5(a,b,c,d,e)          cerr<<#a<<" : "<<a<<" "<<#b<<" : "<<b<<" "<<#c<<" : "<<c<<" "<<#d<<" : "<<d<<" "<<#e<<" : "<<e<<endl
#define t6(a,b,c,d,e,f)          cerr<<#a<<" : "<<a<<" "<<#b<<" : "<<b<<" "<<#c<<" : "<<c<<" "<<#d<<" : "<<d<<" "<<#e<<" : "<<e<<" "<<#f<<" : "<<f<<endl
#define GET_MACRO(_1,_2,_3,_4,_5,_6,NAME,...) NAME
#define t(...) GET_MACRO(__VA_ARGS__,t6,t5, t4, t3, t2, t1)(__VA_ARGS__)
#define _ cerr<<"here"<<endl;
#define __ {ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);}

using namespace std;

int main()
{
  __;
  int n,m,k,p;
  cin>>n>>m>>k>>p;
  vector<vector<int> > v(n,vector<int>(m));
  for(int i=0;i<n;i++) for(int j=0;j<m;j++) cin>>v[i][j];
  priority_queue<ll> a,b;

  for(int i=0;i<n;i++)
  {
    ll sum1 = 0;
    for(int j=0;j<m;j++) sum1+=v[i][j];
    a.push(sum1);
  }

  for(int j=0;j<m;j++)
  {
    ll sum1 = 0;
    for(int i=0;i<n;i++) sum1+=v[i][j];
    b.push(sum1);
  }

  ll subc = 0,subd = 0;
  ll count = 0;

  while(k--)
  {
    ll c = a.top(), d = b.top();
    c-=subc;
    d-=subd;
    if(c>d)
    {
      count+=c;
      a.pop();
      c-=p*m;
      subd+=p;
      a.push(c);
    }
    else
    {
      count+=d;
      b.pop();
      d-=p*n;
      subc+=p;
      b.push(d);
    }
  }

  cout<<count<<endl;
  return 0;
}


/**** [ AkJn ] ****/
#include <bits/stdc++.h>
using namespace std;

#define fastIO ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
#define endl '\n'
#define all(x) (x).begin(),(x).end()
#define vi vector<ll>
#define vii vector <pair<ll,ll> >
#define pb push_back
#define mp make_pair
#define ld long double
#define ll long long
#define min3(a, b, c) min(a,min(b, c))
#define min4(a, b, c, d) min(a, min(b, min(c, d)))
#define max3(a, b, c) max(a,max(b, c))
#define max4(a, b, c, d) max(a, max(b, max(c, d)))
#define yay(x) cout<<endl<<"yay"<<x<<endl;
#define yay1 cout<<"yay1"<<endl;
#define yay2 cout<<"yay2"<<endl;
#define yay3 cout<<"yay3"<<endl;
#define yay4 cout<<"yay4"<<endl;
// #define debug(a) do { std::cout << "Value of " << #a << " is: " << (a) << '\n'; } while(false);
// #define dbg(a) do { std::cout << "Value of " << #a << " is: " << (a) << '\n'; } while(false);
#define dbg(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
#define dbgendl(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); cout<<endl;}
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cerr << *it << " = " << a << endl;
    err(++it, args...);
}
const ll inf = 1e18L + 5;
const ll mod = 1e9+7;

/*
 * THINK MATHEMATICALLY. IT IS NOT AS TOUGH AS IT SEEMS.
 * Check EDGE CASES!! Seriously, even in 2B, saving 8 points by being
 * 2 minutes faster will bite you in the ass if you don't make sure
 * it works on edge cases.
 * Pretests != Tests. READ THIS POINT AGAIN.
 * You do know how to use structs. God damn use them when required.
 * 0 is better than -1.
 * Keep adding to this list.
*/

int main() {
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);

    fastIO
    ll testcases=1;
    // cin>>testcases;
    while(testcases--){
        ll n,m,k,p;
        cin>>n>>m>>k>>p;
        ll a[n][m];
        priority_queue<ll> rowSum, columnSum;

        std::vector<ll> rows(k+1,0), columns(k+1,0);

        for (ll i = 0; i < n; ++i)
        {
            ll curSum=0;
            for (ll j = 0; j < m; ++j)
            {
                cin>>a[i][j];
                curSum += a[i][j];
            }
            rowSum.push(curSum);
        }
        for (ll i = 0; i < m; ++i)
        {
            ll curSum=0;
            for (ll j = 0; j < n; ++j)
            {
                curSum += a[j][i];
            }
            columnSum.push(curSum);
        }

        ll ans = -1e18;

        for (ll i = 1; i <= k; ++i)
        {
            ll x = rowSum.top();
            rowSum.pop();
            rowSum.push(x - m*p);
            rows[i] = rows[i-1]+x;
        }

        for (ll i = 1; i <= k; ++i)
        {
            ll x = columnSum.top();
            columnSum.pop();
            columnSum.push(x - n*p);
            columns[i] = columns[i-1] + x;
        }

        for (ll i = 0; i <= k; ++i)
        {
            ans = max(ans, rows[i]+columns[k-i]-1ll*i*(k-i)*p);
        }

        cout << ans << endl;

    }
    return 0;
}


/**** [ AkJn ] ****/
#include <bits/stdc++.h>
using namespace std;

#define fastIO ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
#define endl '\n'
#define all(x) (x).begin(),(x).end()
#define vi vector<ll>
#define vii vector <pair<ll,ll> >
#define pb push_back
#define mp make_pair
#define ld long double
#define ll long long
#define min3(a, b, c) min(a,min(b, c))
#define min4(a, b, c, d) min(a, min(b, min(c, d)))
#define max3(a, b, c) max(a,max(b, c))
#define max4(a, b, c, d) max(a, max(b, max(c, d)))
#define yay(x) cout<<endl<<"yay"<<x<<endl;
#define yay1 cout<<"yay1"<<endl;
#define yay2 cout<<"yay2"<<endl;
#define yay3 cout<<"yay3"<<endl;
#define yay4 cout<<"yay4"<<endl;
// #define debug(a) do { std::cout << "Value of " << #a << " is: " << (a) << '\n'; } while(false);
// #define dbg(a) do { std::cout << "Value of " << #a << " is: " << (a) << '\n'; } while(false);
#define dbg(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
#define dbgendl(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); cout<<endl;}
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cerr << *it << " = " << a << endl;
    err(++it, args...);
}
const ll inf = 1e18L + 5;
const ll mod = 1e9+7;

/*
 * THINK MATHEMATICALLY. IT IS NOT AS TOUGH AS IT SEEMS.
 * Check EDGE CASES!! Seriously, even in 2B, saving 8 points by being
 * 2 minutes faster will bite you in the ass if you don't make sure
 * it works on edge cases.
 * Pretests != Tests. READ THIS POINT AGAIN.
 * You do know how to use structs. God damn use them when required.
 * 0 is better than -1.
 * Keep adding to this list.
*/

int main() {
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);

    fastIO
    ll testcases=1;
    // cin>>testcases;
    while(testcases--){
        ll n,m,k,p;
        cin>>n>>m>>k>>p;
        ll a[n][m];
        priority_queue<ll> rowSum, columnSum;

        std::vector<ll> rows(k+1,0), columns(k+1,0);

        for (ll i = 0; i < n; ++i)
        {
            ll curSum=0;
            for (ll j = 0; j < m; ++j)
            {
                cin>>a[i][j];
                curSum += a[i][j];
            }
            rowSum.push(curSum);
        }
        for (ll i = 0; i < m; ++i)
        {
            ll curSum=0;
            for (ll j = 0; j < n; ++j)
            {
                curSum += a[j][i];
            }
            columnSum.push(curSum);
        }

        ll ans = -1e18;

        for (ll i = 1; i <= k; ++i)
        {
            ll x = rowSum.top();
            rowSum.pop();
            rowSum.push(x - m*p);
            rows[i] = rows[i-1]+x;
        }

        for (ll i = 1; i <= k; ++i)
        {
            ll x = columnSum.top();
            columnSum.pop();
            columnSum.push(x - n*p);
            columns[i] = columns[i-1] + x;
        }

        for (ll i = 1; i <= k; ++i)
        {
            ans = max(ans, rows[i]+columns[k-i]-1ll*i*(k-i)*p);
        }

        cout << ans << endl;

    }
    return 0;
}


/**** [ AkJn ] ****/
#include <bits/stdc++.h>
using namespace std;

#define fastIO ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
#define endl '\n'
#define all(x) (x).begin(),(x).end()
#define vi vector<ll>
#define vii vector <pair<ll,ll> >
#define pb push_back
#define mp make_pair
#define ld long double
#define ll long long
#define min3(a, b, c) min(a,min(b, c))
#define min4(a, b, c, d) min(a, min(b, min(c, d)))
#define max3(a, b, c) max(a,max(b, c))
#define max4(a, b, c, d) max(a, max(b, max(c, d)))
#define yay(x) cout<<endl<<"yay"<<x<<endl;
#define yay1 cout<<"yay1"<<endl;
#define yay2 cout<<"yay2"<<endl;
#define yay3 cout<<"yay3"<<endl;
#define yay4 cout<<"yay4"<<endl;
// #define debug(a) do { std::cout << "Value of " << #a << " is: " << (a) << '\n'; } while(false);
// #define dbg(a) do { std::cout << "Value of " << #a << " is: " << (a) << '\n'; } while(false);
#define dbg(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
#define dbgendl(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); cout<<endl;}
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cerr << *it << " = " << a << endl;
    err(++it, args...);
}
const ll inf = 1e18L + 5;
const ll mod = 1e9+7;

/*
 * THINK MATHEMATICALLY. IT IS NOT AS TOUGH AS IT SEEMS.
 * Check EDGE CASES!! Seriously, even in 2B, saving 8 points by being
 * 2 minutes faster will bite you in the ass if you don't make sure
 * it works on edge cases.
 * Pretests != Tests. READ THIS POINT AGAIN.
 * You do know how to use structs. God damn use them when required.
 * 0 is better than -1.
 * Keep adding to this list.
*/

int main() {
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);

    fastIO
    ll testcases=1;
    // cin>>testcases;
    while(testcases--){
        ll n,m,k,p;
        cin>>n>>m>>k>>p;
        ll a[n][m];
        priority_queue<ll> rowSum, columnSum;

        for (ll i = 0; i < n; ++i)
        {
            ll curSum=0;
            for (ll j = 0; j < m; ++j)
            {
                cin>>a[i][j];
                curSum += a[i][j];
            }
            rowSum.push(curSum);
        }
        for (ll i = 0; i < m; ++i)
        {
            ll curSum=0;
            for (ll j = 0; j < n; ++j)
            {
                curSum += a[j][i];
            }
            columnSum.push(curSum);
        }

        ll rowCount=0;
        ll colCount=0;

        ll ans = 0;

        while(k--){
            if(rowSum.top()>columnSum.top()){
                ll x = rowSum.top();
                rowSum.pop();
                ans += x - colCount*p;
                rowSum.push(x - m*p);
                rowCount++;
            }
            else{
                ll x = columnSum.top();
                columnSum.pop();
                ans += x - rowCount*p;
                columnSum.push(x - n*p);
                colCount++;
            }
        }

        cout << ans << endl;

    }
    return 0;
}


/**** [ AkJn ] ****/
#include <bits/stdc++.h>
using namespace std;

#define fastIO ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
#define endl '\n'
#define all(x) (x).begin(),(x).end()
#define vi vector<ll>
#define vii vector <pair<ll,ll> >
#define pb push_back
#define mp make_pair
#define ld long double
#define ll long long
#define min3(a, b, c) min(a,min(b, c))
#define min4(a, b, c, d) min(a, min(b, min(c, d)))
#define max3(a, b, c) max(a,max(b, c))
#define max4(a, b, c, d) max(a, max(b, max(c, d)))
#define yay(x) cout<<endl<<"yay"<<x<<endl;
#define yay1 cout<<"yay1"<<endl;
#define yay2 cout<<"yay2"<<endl;
#define yay3 cout<<"yay3"<<endl;
#define yay4 cout<<"yay4"<<endl;
// #define debug(a) do { std::cout << "Value of " << #a << " is: " << (a) << '\n'; } while(false);
// #define dbg(a) do { std::cout << "Value of " << #a << " is: " << (a) << '\n'; } while(false);
#define dbg(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
#define dbgendl(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); cout<<endl;}
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args); }
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cerr << *it << " = " << a << endl;
    err(++it, args...);
}
const ll inf = 1e18L + 5;
const ll mod = 1e9+7;

/*
 * THINK MATHEMATICALLY. IT IS NOT AS TOUGH AS IT SEEMS.
 * Check EDGE CASES!! Seriously, even in 2B, saving 8 points by being
 * 2 minutes faster will bite you in the ass if you don't make sure
 * it works on edge cases.
 * Pretests != Tests. READ THIS POINT AGAIN.
 * You do know how to use structs. God damn use them when required.
 * 0 is better than -1.
 * Keep adding to this list.
*/

int main() {
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);

    fastIO
    ll testcases=1;
    // cin>>testcases;
    while(testcases--){
        ll n,m,k,p;
        cin>>n>>m>>k>>p;
        ll a[n][m];
        priority_queue<ll> rowSum, columnSum;

        std::vector<ll> rows(k+1,0), columns(k+1,0);

        for (ll i = 0; i < n; ++i)
        {
            ll curSum=0;
            for (ll j = 0; j < m; ++j)
            {
                cin>>a[i][j];
                curSum += a[i][j];
            }
            rowSum.push(curSum);
        }
        for (ll i = 0; i < m; ++i)
        {
            ll curSum=0;
            for (ll j = 0; j < n; ++j)
            {
                curSum += a[j][i];
            }
            columnSum.push(curSum);
        }

        ll ans = -1e18;

        for (ll i = 1; i <= k; ++i)
        {
            ll x = rowSum.top();
            rowSum.pop();
            rowSum.push(x - m*p);
            rows[i] = rows[i-1]+x;
        }

        for (ll i = 1; i <= k; ++i)
        {
            ll x = columnSum.top();
            columnSum.pop();
            columnSum.push(x - n*p);
            columns[i] = columns[i-1] + x;
        }

        for (ll i = 0; i < k; ++i)
        {
            ans = max(ans, rows[i]+columns[k-i]-i*(k-i)*p);
        }

        cout << ans << endl;

    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define faster ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
const int MAXN = 1005 ;
typedef long long ll;
typedef pair < ll, int > lli;
int N, M, K, P;
ll A[MAXN][MAXN];
ll h, c;
ll row[MAXN];
ll col[MAXN];
ll sum1[1000005] , sum2[1000005];
void init( ll s[] , int n , int m , ll sum[] )
{
    priority_queue < ll > pq;
    FOR(i, 1, n) pq.push(s[i]);
    FOR(i, 1, K)
    {
        ll val = pq.top();
        pq.pop();
        sum[i] = sum[i - 1] + val;
        pq.push( val - (ll)(P * m) );
    }
}
void solve() {

    init( row , N , M , sum1 );
    init( col , M , N , sum2 );
    ///FOR(i, 1, K) cout << i << " " << sum1[i] << "\n" ;
    ll ans = -1e18;
    FOR(i, 0, K) ans = max( ans , sum1[i] + sum2[K - i] - (ll)( P * i * ( K - i ) ) );
    FOR(i, 0, K) ans = max( ans , sum2[i] + sum1[K - i] - (ll)( P * i * ( K - i ) ) );

    cout << ans;
}
int main() {

//    freopen("446B.INP","r",stdin);
//    freopen("446B.OUT","w",stdout);
    faster
    cin >> N >> M >> K >> P;
    FOR(i, 0, N) { row[i] = 0; }
    FOR(i, 0, M) { col[i] = 0; }
    FOR(i, 0, K) { sum1[i] = sum2[i] = 0; }
    FOR(i, 1, N) FOR(j, 1, M)
    {
        cin >> A[i][j];
        row[i] += A[i][j];
        col[j] += A[i][j];
    }
    solve();
}


#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define faster ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
const int MAXN = 1005 ;
typedef long long ll;
typedef pair < ll, int > lli;
int N, M, K, P;
ll A[MAXN][MAXN];
ll h, c;
ll row[MAXN];
ll col[MAXN];
ll sum1[MAXN] , sum2[MAXN];
void init( ll s[] , int n , int m , ll sum[] )
{
    priority_queue < ll > pq;
    FOR(i, 1, n) pq.push(s[i]);
    FOR(i, 1, K)
    {
        ll val = pq.top();
        pq.pop();
        sum[i] = sum[i-1] + val;
        pq.push( val - P * m );
    }
}
void solve() {

    init( row , N , M , sum1 );
    init( col , M , N , sum2 );
    ///FOR(i, 1, K) cout << i << " " << sum1[i] << "\n" ;
    ll ans = 0;
    FOR(i, 0, K) ans = max( ans , sum1[i] + sum2[K - i] - (ll)( P * i * ( K - i ) ) );
    FOR(i, 0, K) ans = max( ans , sum2[i] + sum1[K - i] - (ll)( P * i * ( K - i ) ) );

    cout << ans;
}
int main() {

//    freopen("446B.INP","r",stdin);
//    freopen("446B.OUT","w",stdout);
    faster
    cin >> N >> M >> K >> P;
    FOR(i, 0, N) { row[i] = 0; }
    FOR(i, 0, M) { col[i] = 0; }
    FOR(i, 0, K) { sum1[i] = sum2[i] = 0; }
    FOR(i, 1, N) FOR(j, 1, M)
    {
        cin >> A[i][j];
        row[i] += A[i][j];
        col[j] += A[i][j];
    }
    solve();
}


#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define faster ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
#define int long long
const int MAXN = 1005 ;
typedef long long ll;
typedef pair < ll, int > lli;
int N, M, K, P;
ll A[MAXN][MAXN];
ll h, c;
ll row[MAXN];
ll col[MAXN];
ll sum1[1000005] , sum2[1000005];
void init( ll s[] , int n , int m , ll sum[] )
{
    priority_queue < ll > pq;
    FOR(i, 1, n) pq.push(s[i]);
    FOR(i, 1, K)
    {
        ll val = pq.top();
        pq.pop();
        sum[i] = sum[i - 1] + val;
        pq.push( val - (ll)(P * m) );
    }
}
void solve() {

    init( row , N , M , sum1 );
    init( col , M , N , sum2 );
    ///FOR(i, 1, K) cout << i << " " << sum1[i] << "\n" ;
    ll ans = -1e18;
    FOR(i, 0, K) ans = max( ans , sum1[i] + sum2[K - i] - (ll)( P * i * ( K - i ) ) );
    FOR(i, 0, K) ans = max( ans , sum2[i] + sum1[K - i] - (ll)( P * i * ( K - i ) ) );

    cout << ans;
}
main() {

//    freopen("446B.INP","r",stdin);
//    freopen("446B.OUT","w",stdout);
    faster
    cin >> N >> M >> K >> P;
    FOR(i, 0, N) { row[i] = 0; }
    FOR(i, 0, M) { col[i] = 0; }
    FOR(i, 0, K) { sum1[i] = sum2[i] = 0; }
    FOR(i, 1, N) FOR(j, 1, M)
    {
        cin >> A[i][j];
        row[i] += A[i][j];
        col[j] += A[i][j];
    }
    solve();
}


#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define faster ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
const int MAXN = 1005 ;
typedef long long ll;
typedef pair < ll, int > lli;
int N, M, K, P;
ll A[MAXN][MAXN];
priority_queue< ll, vector < ll >, less < ll > > qh;
priority_queue< ll, vector < ll >, less < ll > > qc;
ll h, c;
void solve() {
    FOR(i, 1, N) {
        ll ans = 0;
        FOR(j, 1, M) ans += A[i][j];
        //    cout << i << " " << ans << endl;
        qh.push( ans );
    }

    FOR(i, 1, M) {
        ll ans = 0;
        FOR(j, 1, N) ans += A[j][i];
        ///  cout << i << " " << ans << endl;
        qc.push( ans );
    }
    //  cout << qh.top() << " " << qc.top() << endl;
    h = c = 0;
    ll ans = 0;

    while( K-- ) {
        if( qh.top() - h > qc.top() - c ) {
            ll gt = qh.top();
            gt -= P * M;
            ans += qh.top() - h;
            qh.pop();
            qh.push(gt);
            c += P ;
        } else if( qh.top() - h < qc.top() - c ) {
            ll gt = qc.top();
            gt -= P * N;
            ans += qc.top() - c;
            qc.pop();
            qc.push(gt);
            h += P ;
        } else {
            ans += qc.top() - c;

            bool type1, type2;
            // truonwg hop 1 : lay c
            ll gtc = qc.top();
            ll gt1 = gtc - P * N ;
            qc.pop();
            if( qc.empty() == 0 ) gt1 = max( gt1, qc.top() );

            // truong hop 2 : lay h
            ll gth = qh.top();
            ll gt2 = gth - P * M;
            qh.pop();
            if( qh.empty() == 0 ) gt2 = max( gt2, qc.top() );

            if( gth - h > gt1 - c ) type1 = 0;
            else type1 = 1;
            if( gtc - c > gt2 - h ) type2 = 1;
            else type2 = 0;

            if( max( gth - h, gt1 - c ) > max( gtc - c, gt2 - h ) ) {
                if( type1 == 0 ) {
                    gtc -= P * N;
                    qc.push(gtc);
                    h += P;
                } else {
                    gth -= P * M;
                    qc.push(gth);
                    c += P;
                }
            } else {
                if( type2 == 0 ) {
                    gtc -= P * N;
                    qc.push(gtc);
                    h += P;
                } else {
                    gth -= P * M;
                    qc.push(gth);
                    c += P;
                }
            }
        }
        ///  cout << ans << endl;
    }
    cout << ans;
}
int main() {
//    freopen("446B.INP","r",stdin);
//    freopen("446B.OUT","w",stdout);
    faster
    cin >> N >> M >> K >> P;
    FOR(i, 1, N) FOR(j, 1, M) cin >> A[i][j];
    solve();
}


#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define faster ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
const int MAXN = 1005 ;
typedef long long ll;
typedef pair < ll , int > lli;
int N , M , K , P;
ll A[MAXN][MAXN];
priority_queue< ll , vector < ll > , less < ll > > qh;
priority_queue< ll , vector < ll > , less < ll > > qc;
ll h , c;
void solve()
{
    FOR(i, 1, N)
    {
        ll ans = 0;
        FOR(j, 1, M) ans += A[i][j];
    //    cout << i << " " << ans << endl;
        qh.push( ans );
    }

    FOR(i, 1, M)
    {
        ll ans = 0;
        FOR(j, 1, N) ans += A[j][i];
      ///  cout << i << " " << ans << endl;
        qc.push( ans );
    }
  //  cout << qh.top() << " " << qc.top() << endl;
    h = c = 0;
    ll ans = 0;

    while( K-- )
    {
        if( qh.top() - h > qc.top() - c )
        {
            ll gt = qh.top();
            gt -= P * M;
            ans += qh.top() - h;
            qh.pop();
            qh.push(gt);
            c += P ;
        }
        else
        {
            ll gt = qc.top();
            gt -= P * N;
            ans += qc.top() - c;
            qc.pop();
            qc.push(gt);
            h += P ;
        }
      ///  cout << ans << endl;
    }
    cout << ans;
}
int main()
{
//    freopen("446B.INP","r",stdin);
//    freopen("446B.OUT","w",stdout);
    faster
    cin >> N >> M >> K >> P;
    FOR(i, 1, N) FOR(j, 1, M) cin >> A[i][j];
    solve();
}


#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define faster ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
const int MAXN = 1005 ;
typedef long long ll;
typedef pair < ll, int > lli;
int N, M, K, P;
ll A[MAXN][MAXN];
ll h, c;
ll row[MAXN];
ll col[MAXN];
ll sum1[1000005] , sum2[1000005];
void init( ll s[] , int n , int m , ll sum[] )
{
    priority_queue < ll > pq;
    FOR(i, 1, n) pq.push(s[i]);
    FOR(i, 1, K)
    {
        ll val = pq.top();
        pq.pop();
        sum[i] = sum[i-1] + val;
        pq.push( val - (ll)P * m );
    }
}
void solve() {

    init( row , N , M , sum1 );
    init( col , M , N , sum2 );
    ///FOR(i, 1, K) cout << i << " " << sum1[i] << "\n" ;
    ll ans = 0;
    FOR(i, 0, K) ans = max( ans , sum1[i] + sum2[K - i] - (ll)( P * i * ( K - i ) ) );
    FOR(i, 0, K) ans = max( ans , sum2[i] + sum1[K - i] - (ll)( P * i * ( K - i ) ) );

    cout << ans;
}
int main() {

//    freopen("446B.INP","r",stdin);
//    freopen("446B.OUT","w",stdout);
    faster
    cin >> N >> M >> K >> P;
    FOR(i, 0, N) { row[i] = 0; }
    FOR(i, 0, M) { col[i] = 0; }
    FOR(i, 0, K) { sum1[i] = sum2[i] = 0; }
    FOR(i, 1, N) FOR(j, 1, M)
    {
        cin >> A[i][j];
        row[i] += A[i][j];
        col[j] += A[i][j];
    }
    solve();
}


#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define faster ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
const int MAXN = 1005 ;
typedef long long ll;
typedef pair < ll, int > lli;
int N, M, K, P;
ll A[MAXN][MAXN];
ll h, c;
ll row[MAXN];
ll col[MAXN];
ll sum1[MAXN] , sum2[MAXN];
void init( ll s[] , int n , int m , ll sum[] )
{
    priority_queue < ll > pq;
    FOR(i, 1, n) pq.push(s[i]);
    FOR(i, 1, K)
    {
        ll val = pq.top();
        pq.pop();
        sum[i] = sum[i-1] + val;
        pq.push( val - P * m );
    }
}
void solve() {

    init( row , N , M , sum1 );
    init( col , M , N , sum2 );
    ///FOR(i, 1, K) cout << i << " " << sum1[i] << "\n" ;
    ll ans = 0;
    FOR(i, 0, K) ans = max( ans , sum1[i] + sum2[K - i] - (ll)( P * i * ( K - i ) ) );
    FOR(i, 0, K) ans = max( ans , sum2[i] + sum1[K - i] - (ll)( P * i * ( K - i ) ) );

    cout << ans;
}
int main() {

//    freopen("446B.INP","r",stdin);
//    freopen("446B.OUT","w",stdout);
    faster
    cin >> N >> M >> K >> P;
    FOR(i, 0, N) { row[i] = sum1[i] = 0; }
    FOR(i, 0, M) { col[i] = sum2[i] = 0; }

    FOR(i, 1, N) FOR(j, 1, M)
    {
        cin >> A[i][j];
        row[i] += A[i][j];
        col[j] += A[i][j];
    }
    solve();
}


#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define faster ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
const int MAXN = 1005 ;
typedef long long ll;
typedef pair < ll, int > lli;
int N, M, K, P;
ll A[MAXN][MAXN];
ll h, c;
ll row[MAXN];
ll col[MAXN];
ll sum1[MAXN] , sum2[MAXN];
void init( ll s[] , int n , int m , ll sum[] )
{
    priority_queue < int > pq;
    FOR(i, 1, n) pq.push(s[i]);
    FOR(i, 1, K)
    {
        ll val = pq.top();
        pq.pop();
        sum[i] = sum[i-1] + val;
        pq.push( val - P * m );
    }
}
void solve() {

    init( row , N , M , sum1 );
    init( col , M , N , sum2 );
    ///FOR(i, 1, K) cout << i << " " << sum1[i] << "\n" ;
    ll ans = 0;
    FOR(i, 0, K) ans = max( ans , sum1[i] + sum2[K - i] - (ll)( P * i * ( K - i ) ) );
    FOR(i, 0, K) ans = max( ans , sum2[i] + sum1[K - i] - (ll)( P * i * ( K - i ) ) );

    cout << ans;
}
int main() {

//    freopen("446B.INP","r",stdin);
//    freopen("446B.OUT","w",stdout);
    faster
    cin >> N >> M >> K >> P;
    FOR(i, 0, N) { row[i] = sum1[i] = 0; }
    FOR(i, 0, M) { col[i] = sum2[i] = 0; }

    FOR(i, 1, N) FOR(j, 1, M)
    {
        cin >> A[i][j];
        row[i] += A[i][j];
        col[j] += A[i][j];
    }
    solve();
}


#include<bits/stdc++.h>
#define ll long long
#define vi vector<int>
#define vc vector<char>
#define vll vector<ll>
#define pb push_back
#define sf scanf
#define ff first
#define M 1000000007
#define ss second
#define pf printf
#define mp make_pair
#define all(V) V.begin(),V.end()

using namespace std;
ll ans=0;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	ll n,m,k,p;
	cin>>n>>m>>k>>p;
	ll rowsum[1002]={0},colsum[1002]={0};
	ll a;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<m;j++)
		{
			cin>>a;
			rowsum[i]+=a;
			colsum[j]+=a;
		}
	}
	priority_queue<ll>rows,cols,row1;
	priority_queue<ll,vector<ll>,greater<ll> >col1;
	vi row,col;
	for(int i=0;i<n;i++)
	{
		rows.push(rowsum[i]);
	}
	for(int i=0;i<m;i++)
	{
		cols.push(colsum[i]);
	}
	ll sum=0;
	ll i=k;
	row.pb(0);
	while(i--)
	{
		ll a=rows.top();
		sum+=a;
		row.pb(sum);
		rows.pop();
		rows.push(a-m*p);
	}
	i=k;
	sum=0;
	col.pb(0);
	while(i--)
	{
		ll a=cols.top();
		sum+=a;
		col.pb(sum);
		cols.pop();
		cols.push(a-n*p);
	}

	for(int i=0;i<=k;i++)
	{
		ll a=row[k-i];
		ll b=col[i];
		ans=max(ans,a+b-i*(k-i)*p);
	}
	cout<<ans<<endl;
	return 0;
}


#include <iostream>

using namespace std;

int base=998244353;

void xuli(){
    int n,k,a[200001],b[200001],level[200001],res=1,t;
    cin >> n >> k;
    for (int i=1; i<=n; i++) {
        cin >> a[i];
        level[i]=0;
    }
    for (int i=1; i<=k; i++) {
        cin >> t;
        level[t]=i;
    }
    for (int i=1; i<=n; i++){
        if (level[a[i]]==0) continue;
        int tmp=0;
        if (i>1 && level[a[i]]>level[a[i-1]]) tmp++;
        if (i<n && level[a[i]]>level[a[i+1]]) tmp++;
        res=(res*tmp) % base;
    }
    cout << res << endl;
}

int main(){
    int t;
    for (cin >> t; t>0; t--) xuli();
    return 0;
}


#include <cstdio>
#include <cstring>
#define int long long
int n,m;
int a[100010],b[100010],va[100010],vb[100010],l[100010],r[100010],ans=1;
void dfs(int num) {
	if(num==m+1) return ;
	int tm=0;
	if(l[va[b[num]]]&&!vb[a[l[va[b[num]]]]]) {
		r[l[l[va[b[num]]]]]=va[b[num]];
		l[va[b[num]]]=l[l[va[b[num]]]];
		tm+=1;
	}
	if(r[va[b[num]]]<=n&&!vb[a[r[va[b[num]]]]]) {
		if(tm!=1) {
			l[r[r[va[b[num]]]]]=va[b[num]];
			r[va[b[num]]]=r[r[va[b[num]]]];
		}
		tm+=1;
	}
	vb[b[num]]=0;
	ans*=tm;
	ans%=998244353;
	if(ans) dfs(num+1);
}
int main() {
	int T,i;
	scanf("%lld",&T);
	while(T--) {
		memset(va,0,sizeof(va));
		memset(vb,0,sizeof(vb));
		ans=1;
		scanf("%lld%lld",&n,&m);
		for(i=1; i<=n; i++) scanf("%lld",&a[i]),l[i]=i-1,r[i]=i+1,va[a[i]]=i;
		for(i=1; i<=m; i++) scanf("%lld",&b[i]),vb[b[i]]=i;
		dfs(1);
		printf("%lld\n",ans);
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#define int long long
int n,m;
int a[100010],b[100010],va[100010],vb[100010],l[100010],r[100010],ans=1;
void dfs(int num) {
	if(num==m+1) return ;
	int tm=0;
	if(l[va[b[num]]]&&!vb[a[l[va[b[num]]]]]) {
		r[l[l[va[b[num]]]]]=va[b[num]];
		l[va[b[num]]]=l[l[va[b[num]]]];
		tm+=1;
	}
	if(r[va[b[num]]]<=n&&!vb[a[r[va[b[num]]]]]) {
		if(tm!=1) {
			l[r[r[va[b[num]]]]]=va[b[num]];
			r[va[b[num]]]=r[r[va[b[num]]]];
		}
		tm+=1;
	}
	vb[b[num]]=0;
	ans*=tm;
	ans%=998244353;
	if(ans) dfs(num+1);
}
signed main() {
	int T,i;
	scanf("%lld",&T);
	while(T--) {
		memset(va,0,sizeof(va));
		memset(vb,0,sizeof(vb));
		ans=1;
		scanf("%lld%lld",&n,&m);
		for(i=1; i<=n; i++) scanf("%lld",&a[i]),l[i]=i-1,r[i]=i+1,va[a[i]]=i;
		for(i=1; i<=m; i++) scanf("%lld",&b[i]),vb[b[i]]=i;
		dfs(1);
		printf("%lld\n",ans);
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#define int long long
int n,m;
int a[200010],b[200010],va[200010],vb[200010],l[200010],r[200010],ans=1;
void dfs(int num) {
	if(num==m+1) return ;
	int tm=0;
	if(l[va[b[num]]]&&!vb[a[l[va[b[num]]]]]) {
		r[l[l[va[b[num]]]]]=va[b[num]];
		l[va[b[num]]]=l[l[va[b[num]]]];
		tm+=1;
	}
	if(r[va[b[num]]]<=n&&!vb[a[r[va[b[num]]]]]) {
		if(tm!=1) {
			l[r[r[va[b[num]]]]]=va[b[num]];
			r[va[b[num]]]=r[r[va[b[num]]]];
		}
		tm+=1;
	}
	vb[b[num]]=0;
	ans*=tm;
	ans%=998244353;
	if(ans) dfs(num+1);
}
signed main() {
	int T,i;
	scanf("%lld",&T);
	while(T--) {
		memset(va,0,sizeof(va));
		memset(vb,0,sizeof(vb));
		ans=1;
		scanf("%lld%lld",&n,&m);
		for(i=1; i<=n; i++) scanf("%lld",&a[i]),l[i]=i-1,r[i]=i+1,va[a[i]]=i;
		for(i=1; i<=m; i++) scanf("%lld",&b[i]),vb[b[i]]=i;
		dfs(1);
		printf("%lld\n",ans);
	}
	return 0;
}


#include<bits/stdc++.h>

#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define sc(a) scanf("%c",&a)
#define ss(a) scanf("%s",a)
#define pi(a) printf("%d\n",a)
#define pl(a) printf("%lld\n",a)
#define pc(a) putchar(a)
#define ms(a) memset(a,0,sizeof(a))
#define repi(i, a, b) for(int i=a;i<=b;++i)
#define repd(i, a, b) for(register int i=a;i>=b;--i)
#define reps(s) for(register int i=head[s];i;i=Next[i])
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define pii pair<int,int>
#define mii unordered_map<int,int>
#define msi unordered_map<string,int>
#define lowbit(x) ((x)&(-(x)))
#define ce(i, r) i==r?'\n':' '
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define INF 0x3f3f3f3f
#define pr(x) cout<<#x<<": "<<x<<endl
using namespace std;

inline int qr() {
    int f = 0, fu = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-')fu = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        f = (f << 3) + (f << 1) + c - 48;
        c = getchar();
    }
    return f * fu;
}

const int N = 2e5 + 10, MOD = 998244353;

struct UF {
    int fa[N], l[N], r[N];

    inline void init(int n) {
        for (int i = 1; i <= n; i++)l[i] = r[i] = fa[i] = i;
    }

    int get(int x) {
        return x == fa[x] ? x : (fa[x] = get(fa[x]));
    }

    inline void merge(int x, int y) {
        x = get(x), y = get(y);
        l[x] = min(l[x], l[y]);
        r[x] = max(r[x], r[y]);
        fa[y] = x;
    }
} U;

bool used[N];
int a[N], b[N], pos[N], n, m, T;

int main() {
    T = qr();
    while (T--) {
        n = qr(), m = qr();
        repi(i, 1, n)pos[a[i] = qr()] = i, used[a[i]] = false;
        repi(i, 1, m)used[b[i] = qr()] = true;
        U.init(n);
        int ans = 1;
        repi(i, 1, m) {
            int p = U.get(pos[b[i]]), l, r;
            bool okl = false, okr = false;
            if (U.l[p] > 1)okl = !used[a[U.get(U.l[p] - 1)]];
            if (U.r[p] < n)okr = !used[a[U.get(U.r[p] + 1)]];
            if (!okl && !okr) {
                ans = 0;
                break;
            }
            used[b[i]] = false;
            if (okl && okr)ans = (ans << 1) % MOD, U.merge(p, l);
            else U.merge(p, okl ? r : l);
        }
        pi(ans);
    }
    return 0;
}


#include<bits/stdc++.h>

#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define sc(a) scanf("%c",&a)
#define ss(a) scanf("%s",a)
#define pi(a) printf("%d\n",a)
#define pl(a) printf("%lld\n",a)
#define pc(a) putchar(a)
#define ms(a) memset(a,0,sizeof(a))
#define repi(i, a, b) for(int i=a;i<=b;++i)
#define repd(i, a, b) for(register int i=a;i>=b;--i)
#define reps(s) for(register int i=head[s];i;i=Next[i])
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define pii pair<int,int>
#define mii unordered_map<int,int>
#define msi unordered_map<string,int>
#define lowbit(x) ((x)&(-(x)))
#define ce(i, r) i==r?'\n':' '
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define INF 0x3f3f3f3f
#define pr(x) cout<<#x<<": "<<x<<endl
using namespace std;

inline int qr() {
    int f = 0, fu = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-')fu = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        f = (f << 3) + (f << 1) + c - 48;
        c = getchar();
    }
    return f * fu;
}

const int N = 2e5 + 10, MOD = 998244353;

struct UF {
    int fa[N], l[N], r[N];

    inline void init(int n) {
        for (int i = 1; i <= n; i++)l[i] = r[i] = fa[i] = i;
    }

    int get(int x) {
        return x == fa[x] ? x : (fa[x] = get(fa[x]));
    }

    inline void merge(int x, int y) {
        x = get(x), y = get(y);
        l[x] = min(l[x], l[y]);
        r[x] = max(r[x], r[y]);
        fa[y] = x;
    }

    inline bool same(int x, int y) {
        return get(x) == get(y);
    }
} U;

bool used[N];
int a[N], b[N], pos[N], n,m, T;

int main() {
    T = qr();
    while (T--) {
        n = qr(),m=qr();
        repi(i, 1, n)pos[a[i] = qr()] = i, used[a[i]] = false;
        repi(i, 1, m)used[b[i] = qr()] = true;
        U.init(n);
        int ans = 1;
        repi(i, 1, m) {
            int p = U.get(pos[b[i]]), l, r;
            bool okl = false, okr = false;
            if (U.l[p] > 1) {
                l = U.get(U.l[p] - 1);
//                pr(a[l]);
                if (!used[a[l]])okl = true;
            }
            if (U.r[p] < n) {
                r = U.get(U.r[p] + 1);
//                pr(a[r]);
                if (!used[a[r]])okr = true;
            }
//            pr(okl), pr(okr);
            if (!okl && !okr) {
                ans = 0;
                break;
            }
            used[b[i]] = false;
            if (okl && okr)ans = (ans << 1) % MOD, U.merge(p, l);
            else U.merge(p, okl ? r : l);
        }
        pi(ans);
    }
    return 0;
}


#include<bits/stdc++.h>

#define si(a) scanf("%d",&a)
#define sl(a) scanf("%lld",&a)
#define sd(a) scanf("%lf",&a)
#define sc(a) scanf("%c",&a)
#define ss(a) scanf("%s",a)
#define pi(a) printf("%d\n",a)
#define pl(a) printf("%lld\n",a)
#define pc(a) putchar(a)
#define ms(a) memset(a,0,sizeof(a))
#define repi(i, a, b) for(int i=a;i<=b;++i)
#define repd(i, a, b) for(register int i=a;i>=b;--i)
#define reps(s) for(register int i=head[s];i;i=Next[i])
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define pii pair<int,int>
#define mii unordered_map<int,int>
#define msi unordered_map<string,int>
#define lowbit(x) ((x)&(-(x)))
#define ce(i, r) i==r?'\n':' '
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(),x.end()
#define INF 0x3f3f3f3f
#define pr(x) cout<<#x<<": "<<x<<endl
using namespace std;

inline int qr() {
    int f = 0, fu = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-')fu = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        f = (f << 3) + (f << 1) + c - 48;
        c = getchar();
    }
    return f * fu;
}

const int N = 2e5 + 10, MOD = 998244353;

struct UF {
    int fa[N], l[N], r[N];

    inline void init(int n) {
        for (int i = 1; i <= n; i++)l[i] = r[i] = fa[i] = i;
    }

    int get(int x) {
        return x == fa[x] ? x : (fa[x] = get(fa[x]));
    }

    inline void merge(int x, int y) {
        x = get(x), y = get(y);
        l[x] = min(l[x], l[y]);
        r[x] = max(r[x], r[y]);
        fa[y] = x;
    }
} U;

bool used[N];
int a[N], b[N], pos[N], n, m, T;

int main() {
    T = qr();
    while (T--) {
        n = qr(), m = qr();
        repi(i, 1, n)pos[a[i] = qr()] = i, used[a[i]] = false;
        repi(i, 1, m)used[b[i] = qr()] = true;
        U.init(n);
        int ans = 1;
        repi(i, 1, m) {
            int p = U.get(pos[b[i]]), l, r;
            bool okl = false, okr = false;
            if (U.l[p] > 1)okl = !used[a[l=U.get(U.l[p] - 1)]];
            if (U.r[p] < n)okr = !used[a[r=U.get(U.r[p] + 1)]];
            if (!okl && !okr) {
                ans = 0;
                break;
            }
            used[b[i]] = false;
            if (okl && okr)ans = (ans << 1) % MOD, U.merge(p, l);
            else U.merge(p, okl ? l : r);
        }
        pi(ans);
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std ;
const int N = 2e5 + 10 ;
int n , m ;
bool vis[N] , in[N] ;
int undec[N] , a[N] , b[N] , which[N] ;

int main () {
	int ti ;
	cin >> ti ;
	while (ti --) {
		memset (vis , 0 , sizeof vis) ;
		memset (which , 0 , sizeof which) ;
		memset (undec , 0 , sizeof undec) ;
		memset (in , 0 , sizeof in) ;
		cin >> n >> m ;
		for (int i = 1 ; i <= n ; i ++) cin >> a[i] ;
		for (int i = 1 ; i <= m ; i ++) {
			cin >> b[i] ;
			vis[b[i]] = 1 ;
			which[b[i]] = i ;
		}
		undec[0] = 0x3f3f3f3f , undec[n + 1] = 0x3f3f3f3f  ;
		for (int i = 1 ; i <= n ; i ++) if (vis[a[i]]) undec[i] = which[a[i]] , in[a[i]] = 1 ;
		long long ans = 1 ;
		for (int i = 1 ; i <= n ; i ++) {
			int par = 0 ;
			if (in[a[i]]) {
				if (undec[i - 1] < undec[i]) par ++ ;
				if (undec[i + 1] < undec[i]) par ++ ;
				ans = (par * ans) % 998244353 ;
			}
		}
		cout << ans << endl ;
	}
	return 0 ;
}


#include <bits/stdc++.h>
using namespace std ;
const int N = 2e5 + 10 ;

int qwq () {
	int x = 0 , f = 1 ;
	char ch = getchar () ;
	while (ch < '0' || ch > '9') {
		if (ch == '-') f = -1 ;
		ch = getchar () ;
 	}
 	while (ch >= '0' && ch <= '9') {
 		x = x * 10 + ch - '0' ;
		ch = getchar () ;
	}
	return x * f ;
}

int n , m ;
bool vis[N] , in[N] ;
int undec[N] , a[N] , b[N] , which[N] ;

int main () {
	int ti ;
	ti = qwq () ;
	while (ti --) {
		memset (vis , 0 , sizeof vis) ;
		memset (which , 0 , sizeof which) ;
		memset (undec , 0 , sizeof undec) ;
		memset (in , 0 , sizeof in) ;
		n = qwq () , m = qwq ()  ;
		for (int i = 1 ; i <= n ; i ++) a[i] = qwq () ;
		for (int i = 1 ; i <= m ; i ++) {
 			b[i] = qwq () ;
			vis[b[i]] = 1 ;
			which[b[i]] = i ;
		}
		undec[0] = 0x3f3f3f3f , undec[n + 1] = 0x3f3f3f3f  ;
		for (int i = 1 ; i <= n ; i ++) if (vis[a[i]]) undec[i] = which[a[i]] , in[a[i]] = 1 ;
		long long ans = 1 ;
		for (int i = 1 ; i <= n ; i ++) {
			int par = 0 ;
			if (in[a[i]]) {
				if (undec[i - 1] < undec[i]) par ++ ;
				if (undec[i + 1] < undec[i]) par ++ ;
				ans = (par * ans) % 998244353 ;
			}
		}
		cout << ans << endl ;
	}
	return 0 ;
}


#include <bits/stdc++.h>
using namespace std ;
const int N = 2e5 + 10 ;

int qwq () {
	int x = 0 , f = 1 ;
	char ch = getchar () ;
	while (ch < '0' || ch > '9') {
		if (ch == '-') f = -1 ;
		ch = getchar () ;
 	}
 	while (ch >= '0' && ch <= '9') {
 		x = x * 10 + ch - '0' ;
		ch = getchar () ;
	}
	return x * f ;
}

int n , m ;
bool vis[N] , in[N] ;
int undec[N] , a[N] , b[N] , which[N] ;

int main () {
	int ti ;
	ti = qwq () ;
	while (ti --) {
		memset (vis , 0 , sizeof vis) ;
		memset (in , 0 , sizeof in) ;
		n = qwq () , m = qwq ()  ;
		for (int i = 1 ; i <= n ; i ++) a[i] = qwq () ;
		for (int i = 1 ; i <= m ; i ++) {
 			b[i] = qwq () ;
			vis[b[i]] = 1 ;
			which[b[i]] = i ;
		}
		undec[0] = 0x3f3f3f3f , undec[n + 1] = 0x3f3f3f3f  ;
		for (int i = 1 ; i <= n ; i ++) if (vis[a[i]]) undec[i] = which[a[i]] , in[a[i]] = 1 ;
		long long ans = 1 ;
		for (int i = 1 ; i <= n ; i ++) {
			int par = 0 ;
			if (in[a[i]]) {
				if (undec[i - 1] < undec[i]) par ++ ;
				if (undec[i + 1] < undec[i]) par ++ ;
				ans = (par * ans) % 998244353 ;
			}
		}
		cout << ans << endl ;
	}
	return 0 ;
}


#include <bits/stdc++.h>
using namespace std ;
const int N = 2e5 + 10 ;
#define ms(a) memset (a , 0 , sizeof a)
int qwq () {
	int x = 0 , f = 1 ;
	char ch = getchar () ;
	while (ch < '0' || ch > '9') {
		if (ch == '-') f = -1 ;
		ch = getchar () ;
 	}
 	while (ch >= '0' && ch <= '9') {
 		x = x * 10 + ch - '0' ;
		ch = getchar () ;
	}
	return x * f ;
}

int n , m ;
bool vis[N] , in[N] ;
int undec[N] , a[N] , b[N] , which[N] ;

int main () {
	int ti ;
	ti = qwq () ;
	while (ti --) {

//		ms (in) ;
//		ms (undec) ;
//		ms (which) ;
//		ms (vis) ;
		n = qwq () , m = qwq ()  ;
		for (int i = 1 ; i <= n ; i ++) a[i] = qwq () ;
		for (int i = 1 ; i <= m ; i ++) {
 			b[i] = qwq () ;
			vis[b[i]] = 1 ;
			which[b[i]] = i ;
		}
		undec[0] = 0x3f3f3f3f , undec[n + 1] = 0x3f3f3f3f  ;
		for (int i = 1 ; i <= n ; i ++) if (vis[a[i]]) undec[i] = which[a[i]] , in[a[i]] = 1 ;
		long long ans = 1 ;
		for (int i = 1 ; i <= n ; i ++) {
			int par = 0 ;
			if (in[a[i]]) {
				if (undec[i - 1] < undec[i]) par ++ ;
				if (undec[i + 1] < undec[i]) par ++ ;
				ans = (par * ans) % 998244353 ;
			}
		}
		cout << ans * 1ll << endl ;

		for (int i = 0 ; i <= n + 1 ; i ++) {
			which[i] = 0 , vis[i] = 0 , in[i] = 0 , undec[i] = 0 ;

		}
	}
	return 0 ;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define fi first
#define se second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

#define IN(i, x) for (auto &(i):(x))
#define FIT(i, x) for (auto (i)=(x).begin(); (i)!=(x).end(); ++(i))
#define RIT(i, x) for (auto (i)=(x).rbegin(); (i)!=(x).rend(); ++(i))
#define finish(...) return void(print(__VA_ARGS__))

using ll=long long;
using db=double;
using ld=long double;

using pii=pair<int, int>;
using pll=pair<ll, ll>;
using pdd=pair<db, db>;

using vi=vector<int>;
using vl=vector<ll>;
using vd=vector<db>;
using vs=vector<string>;
using vii=vector<pii>;
using vll=vector<pll>;
using vdd=vector<pdd>;

template<class T> using pqueue=priority_queue<T>;
template<class T> using mqueue=priority_queue<T, vector<T>, greater<T>>;
template<class T> using oset=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

template<class T> bool ckmin(T &a, const T &b) { return (b<a?a=b, 1:0); }
template<class T> bool ckmax(T &a, const T &b) { return (a<b?a=b, 1:0); }

template<class T> void read(T &x) { cin >> x; }
template<class H, class T> void read(pair<H, T> &p) { cin >> p.fi >> p.se; }
template<class T, size_t S> void read(array<T, S> &a) { IN(i, a) read(i); }

template<class T> void read(vector<T> &v) { IN(i, v) read(i); }
template<class H, class... T> void read(H &h, T &...t) { read(h); read(t...); }

template<class H, class T> ostream &operator<<(ostream &o, pair<H, T> &p) { o << p.fi << " " << p.se; return o; }
template<class H, class T> ostream &operator<<(ostream &o, vector<pair<H, T>> &v) { string s; IN(i, v) o << s << i, s="\n"; return o; }
template<class T, size_t S> ostream &operator<<(ostream &o, array<T, S> &a) { string s; IN(i, a) o << s << i, s=" "; return o; }
template<class T, size_t S> ostream &operator<<(ostream &o, vector<array<T, S>> &v) { string s; IN(i, v) o << s << i, s="\n"; return o; }

template<class T> ostream &operator<<(ostream &o, vector<T> &v) { string s; IN(i, v) o << s << i, s=" "; return o; }
template<class T> ostream &operator<<(ostream &o, vector<vector<T>> &v) { string s; IN(i, v) o << s << i, s="\n"; return o; }

template<class T> void write(T x) { cout << x; }
template<class H, class... T> void write(const H &h, const T &...t) { write(h); write(t...); }

void print() { write('\n'); }
template<class H, class... T> void print(const H &h, const T &...t) { write(h); if (sizeof...(t)) write(' '); print(t...); }

const ll M=998244353;
const int dx[]={0,1,0,-1}, dy[]={1,0,-1,0};

void solve() {
    int n, k;
    read(n, k);
    vi a(n); set<int> b;
    read(a);
    for (int i=0; i<k; ++i) {
        int x; read(x);
        b.insert(x);
    }
    if (n==1)
        finish(0);
    ll ans=1;
    for (int i=0; i<n; ++i) {
        if (!b.count(a[i]))
            continue;
        if (i && i<n-1) {
            if (b.count(a[i-1]) && b.count(a[i+1]))
                finish(0);
            if (!b.count(a[i-1]) && !b.count(a[i+1]))
                ans=ans*2%M;
        }
        else if (!i && b.count(a[1]))
            finish(0);
        else if (i==n-1 && b.count(a[n-2]))
            finish(0);
        b.erase(a[i]);
    }
    print(ans);
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    int t; read(t);
    while (t--)
        solve();
    cerr << "Time elapsed: " << 1.0*clock()/CLOCKS_PER_SEC << "s\n";
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

#define fi first
#define se second
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

#define IN(i, x) for (auto &(i):(x))
#define FIT(i, x) for (auto (i)=(x).begin(); (i)!=(x).end(); ++(i))
#define RIT(i, x) for (auto (i)=(x).rbegin(); (i)!=(x).rend(); ++(i))
#define finish(...) return void(print(__VA_ARGS__))

using ll=long long;
using db=double;
using ld=long double;

using pii=pair<int, int>;
using pll=pair<ll, ll>;
using pdd=pair<db, db>;

using vi=vector<int>;
using vl=vector<ll>;
using vd=vector<db>;
using vs=vector<string>;
using vii=vector<pii>;
using vll=vector<pll>;
using vdd=vector<pdd>;

template<class T> using pqueue=priority_queue<T>;
template<class T> using mqueue=priority_queue<T, vector<T>, greater<T>>;
template<class T> using oset=tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

template<class T> bool ckmin(T &a, const T &b) { return (b<a?a=b, 1:0); }
template<class T> bool ckmax(T &a, const T &b) { return (a<b?a=b, 1:0); }

template<class T> void read(T &x) { cin >> x; }
template<class H, class T> void read(pair<H, T> &p) { cin >> p.fi >> p.se; }
template<class T, size_t S> void read(array<T, S> &a) { IN(i, a) read(i); }

template<class T> void read(vector<T> &v) { IN(i, v) read(i); }
template<class H, class... T> void read(H &h, T &...t) { read(h); read(t...); }

template<class H, class T> ostream &operator<<(ostream &o, pair<H, T> &p) { o << p.fi << " " << p.se; return o; }
template<class H, class T> ostream &operator<<(ostream &o, vector<pair<H, T>> &v) { string s; IN(i, v) o << s << i, s="\n"; return o; }
template<class T, size_t S> ostream &operator<<(ostream &o, array<T, S> &a) { string s; IN(i, a) o << s << i, s=" "; return o; }
template<class T, size_t S> ostream &operator<<(ostream &o, vector<array<T, S>> &v) { string s; IN(i, v) o << s << i, s="\n"; return o; }

template<class T> ostream &operator<<(ostream &o, vector<T> &v) { string s; IN(i, v) o << s << i, s=" "; return o; }
template<class T> ostream &operator<<(ostream &o, vector<vector<T>> &v) { string s; IN(i, v) o << s << i, s="\n"; return o; }

template<class T> void write(T x) { cout << x; }
template<class H, class... T> void write(const H &h, const T &...t) { write(h); write(t...); }

void print() { write('\n'); }
template<class H, class... T> void print(const H &h, const T &...t) { write(h); if (sizeof...(t)) write(' '); print(t...); }

const ll M=998244353;
const int dx[]={0,1,0,-1}, dy[]={1,0,-1,0};

void solve() {
    int n, k;
    read(n, k);
    vi at(n+1), b(n+1), take(n+2);
    take[0]=take[n+1]=1;
    for (int i=1; i<=n; ++i) {
        int x; read(x);
        at[x]=i;
    }
    for (int i=0; i<k; ++i) {
        read(b[i]);
        take[at[b[i]]]=1;
    }
    ll ans=1;
    IN(i, b) {
        if (take[at[i]-1] && take[at[i]+1])
            finish(0);
        if (!take[at[i]-1] && !take[at[i]+1])
            ans=ans*2%M;
        take[at[i]]=0;
    }
    print(ans);
}

int main() {
    cin.tie(0)->sync_with_stdio(0);
    int t; read(t);
    while (t--)
        solve();
    cerr << "Time elapsed: " << 1.0*clock()/CLOCKS_PER_SEC << "s\n";
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair
typedef pair<ll, ll>  pll;
typedef pair<int, int> pii;

const int maxn=2e5+10;
int a[maxn],b[maxn],id[maxn];
const ll mod=998244353;
int main()
{
	IOS;
	int T;
	cin>>T;
	while(T--){
		int n,k;
		cin>>n>>k;
		set<int> S;
		for(int i=1;i<=n;i++){
			cin>>a[i];
			id[a[i]]=i;
		}
		for(int i=1;i<=k;i++){
			cin>>b[i];
			S.insert(b[i]);
		}
		ll ans=1;
		for(int i=1;i<=k;i++){
			int cnt=id[b[i]];
			int m=0;
			if(cnt-1>=1){
				if(S.count(a[cnt-1])==0){
					m++;
				}
			}
			if(cnt+1<=n){
				if(S.count(a[cnt+1])==0){
					m++;
				}
			}
			S.erase(b[i]);
			ans=(ans*m)%mod;
		}
		cout<<ans<<endl;
	}
}


/**
 * Prof.Nicola
**/
#include <bits/stdc++.h>

using namespace std;
vector<long long>tree;
long long mod=998244353;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    long long n,k,x,z,t;
    cin>>t;
    for(long j=0;j<t;j++){
    cin>>n>>k;
    vector<long long>a(n),b(k),ok(n,1),pos(n);
    for(long i=0;i<n;i++){
        cin>>a[i];
        pos[a[i]-1]=i;
    }
    for(long i=0;i<k;i++){
        cin>>b[i];
        ok[b[i]-1]=false;
    }
    long long ans=1,v;
    for(long i=0;i<k;i++){
        x=pos[b[i]-1];
        v=0;
        if(x<a.size()-1&&ok[a[x+1]-1]){
            v++;
        }
        if(x>0&&ok[a[x-1]-1]){
            v++;
        }
        ans=(ans*v)%mod;
        ok[b[i]-1]=true;
    }
    cout<<ans<<endl;
    }
    return 0;
}


/**
 * Prof.Nicola
**/
#include <bits/stdc++.h>

using namespace std;
vector<long long>tree;
long long mod=998244353;
long long power(long long x,long long p)
{
    if(p==0){
        return 1;
    }
    long long z=power(x,p/2);
    z=(z*z)%mod;
    if(p%2==1){
        z=(z*x)%mod;
    }
    return z;
}
long long power_of_two(long long x)
{
    long long z=0;
    while(x>1){
        x/=2;
        z++;
    }
    return z;
}
void segment_tree(vector<long long>v)
{
    long long x=v.size();
    x=power_of_two(x);
    x=power(2,x);
    if(v.size()!=x){
        x*=2;
    }
    tree.resize(x*2);
    tree[0]=x;
    for(long i=0;i<v.size();i++){
        tree[i+x]=v[i];
    }
    for(long i=x-1;i>0;i--){
        tree[i]=tree[i*2]+tree[(i*2)+1];
    }
}
long long seg(long long l,long long r)
{
    l+=tree[0];
    r+=tree[0];
    long long ans=0;
    while(l<=r){
        if(l%2==1){
            ans+=tree[l];
        }
        if(r%2==0){
            ans+=tree[r];
        }
        l++;
        l/=2;
        r--;
        r/=2;
    }
    return ans;
}
void update(long long x,long long z)
{
    x+=tree[0];
    tree[x]=z;
    x/=2;
    while(x>0){
        tree[x]=tree[x*2]+tree[(x*2)+1];
        x/=2;
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    long long n,k,x,z,t;
    cin>>t;
    for(long j=0;j<t;j++){
    cin>>n>>k;
    vector<long long>a(n),b(k),ok(n,1),pos(n),com(n,1);
    tree.clear();
    segment_tree(a);
    for(long i=0;i<n;i++){
        cin>>a[i];
        pos[a[i]-1]=i;
    }
    for(long i=0;i<k;i++){
        cin>>b[i];
        com[b[i]-1]=0;
        ok[b[i]-1]=false;
    }
    long long ans=0,v,q;
    for(long i=0;i<k;i++){
        x=seg(0,pos[b[i]-1]);
        x=pos[b[i]-1]-x;
        v=0;
        q=1;
        if(x<a.size()-1&&ok[a[x+1]-1]){
            v++;
            q*=com[a[x+1]-1];
            q%=mod;
            update(pos[a[x+1]-1],1);
            a.erase(a.begin()+x+1);
        }
        if(x>0&&ok[a[x-1]-1]){
            v++;
            q*=com[a[x-1]-1];
            q%=mod;
            update(pos[a[x-1]-1],1);
            a.erase(a.begin()+x-1);
            x--;
        }
        com[b[i]-1]+=q;
        com[b[i]-1]%=mod;
        com[b[i]-1]*=v;
        com[b[i]-1]%=mod;
        ok[b[i]-1]=true;
    }
    cout<<com[b[k-1]-1]<<endl;
    }
    return 0;
}


/**
 * Prof.Nicola
**/
#include <bits/stdc++.h>

using namespace std;
vector<long long>tree;
long long mod=998244353;
long long power(long long x,long long p)
{
    if(p==0){
        return 1;
    }
    long long z=power(x,p/2);
    z=(z*z)%mod;
    if(p%2==1){
        z=(z*x)%mod;
    }
    return z;
}
long long power_of_two(long long x)
{
    long long z=0;
    while(x>1){
        x/=2;
        z++;
    }
    return z;
}
void segment_tree(vector<long long>v)
{
    long long x=v.size();
    x=power_of_two(x);
    x=power(2,x);
    if(v.size()!=x){
        x*=2;
    }
    tree.resize(x*2);
    tree[0]=x;
    for(long i=0;i<v.size();i++){
        tree[i+x]=v[i];
    }
    for(long i=x-1;i>0;i--){
        tree[i]=tree[i*2]+tree[(i*2)+1];
    }
}
long long seg(long long l,long long r)
{
    l+=tree[0];
    r+=tree[0];
    long long ans=0;
    while(l<=r){
        if(l%2==1){
            ans+=tree[l];
        }
        if(r%2==0){
            ans+=tree[r];
        }
        l++;
        l/=2;
        r--;
        r/=2;
    }
    return ans;
}
void update(long long x,long long z)
{
    x+=tree[0];
    tree[x]=z;
    x/=2;
    while(x>0){
        tree[x]=tree[x*2]+tree[(x*2)+1];
        x/=2;
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    long long n,k,x,z,t;
    cin>>t;
    for(long j=0;j<t;j++){
    cin>>n>>k;
    vector<long long>a(n),b(k),ok(n,1),pos(n);
    tree.clear();
    segment_tree(a);
    for(long i=0;i<n;i++){
        cin>>a[i];
        pos[a[i]-1]=i;
    }
    for(long i=0;i<k;i++){
        cin>>b[i];
        ok[b[i]-1]=false;
    }
    long long ans=1,v;
    for(long i=0;i<k;i++){
        x=seg(0,pos[b[i]-1]);
        x=pos[b[i]-1]-x;
        v=0;
        if(x<a.size()-1&&ok[a[x+1]-1]){
            v++;
            update(pos[a[x+1]-1],1);
            a.erase(a.begin()+x+1);
        }
        if(x>0&&ok[a[x-1]-1]){
            v++;
            update(pos[a[x-1]-1],1);
            a.erase(a.begin()+x-1);
        }
        ans=(ans*v)%mod;
        ok[b[i]-1]=true;
    }
    cout<<ans<<endl;
    }
    return 0;
}


/**
 * Prof.Nicola
**/
#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    long long t,m,d,w,x,z;
    cin>>t;
    for(long i=0;i<t;i++){
        cin>>m>>d>>w;
        x=w/__gcd(d-1,w);
        z=min(m,d)%x;
        x=min(m,d)/x;
        cout<<(((x*(x+1))/2)*z)+(((x*(x-1))/2)*((w/__gcd(d-1,w))-z))<<endl;
    }
    return 0;
}


#include <cstdio>
#include <cstring>
#include <algorithm>
#define mod 998244353

int a[200010], b[200010], c[200010], r[200010], loc[200010];
bool locked[200010];

int main() {
    int t;
    scanf("%d", &t);
    while(t--) {
        int n, k;
        scanf("%d %d", &n, &k);
        for(int i = 0; i < n; ++i)  scanf("%d", &a[i]);
        for(int i = 0; i < k; ++i) { scanf("%d", &b[i]);  c[i] = b[i];  r[i] = i; }
        std::sort(c, c+k);
        std::sort(r, r+k, [](int x, int y) { return b[x] < b[y]; });
        memset(locked, 0, sizeof(locked));
        for(int i = 0; i < n; ++i) {
            auto it = std::lower_bound(c, c+k, a[i]);
            if(it != c+k && *it == a[i]) {
                loc[r[it-c]] = i;
                locked[i] = true;
            }
        }
        int ans = 1;
        for(int i = 0; i < k; ++i) {
            ans = (ans * ((loc[i] > 0 && !locked[loc[i]-1]) + (loc[i] < n-1 && !locked[loc[i]+1]))) % mod;
            locked[loc[i]] = false;
        }
        printf("%d\n", ans);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int read(){
	int a=0,b=getchar();
	while(!isdigit(b))b=getchar();
	while(isdigit(b))a=a*10+b-'0',b=getchar();
	return a;
}
const ll N=2e5+5,mod=998244353;
ll T,n,k,ans,a[N],b[N],c[N],d[N];
int main(){
	T=read();
	while(T--){
		n=read(),k=read(),ans=1;
		for(int i=0;i<n;i++)
			a[i]=read(),c[a[i]]=i;
		for(int i=0;i<k;i++)
			b[i]=read(),d[b[i]]=1;
		for(int i=0;i<k;i++){
			ll x=c[b[i]],y=0;
			if(x>0 and !d[a[x-1]])y++;
			if(x<n-1 and !d[a[x+1]])y++;
			(ans*=y)%=mod;
			d[a[x]]=0;
		}
		cout << ans << endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 100;
int a[maxn], vis[maxn];
int c[maxn], vc[maxn];
const int mod = 1e9 + 7;
int main(){
	int T;
	cin >> T;
	while (T--){
		int n, k;
		cin >> n >> k;
		for (int i = 0; i <= n + 10; i++)
			vis[i] = 0, vc[i] = 0;
		for (int i = 1; i <= n; i++){
			scanf("%d", &a[i]);
			vis[a[i]] = i;
		}
		for (int i = 1; i <= k; i++){
			scanf("%d", &c[i]);
			vc[vis[c[i]]] = 1;
		}
		ll ans = 1;
		int flag = 0;
		vc[0] = vc[n + 1] = 1;
		for (int i = 1; i <= k; i++){
			int x = c[i], w = vis[c[i]];
			if (vc[w - 1] && vc[w + 1]){
				flag = 1;
				break;
			}
			else if (w == n&&vc[w-1]){
				flag = 1;
				break;
			}
			else if (w==n) continue;
			else if (w == 1 && vc[w + 1]){
				flag = 1;
				break;
			}
			else if (w==1) continue;
			else if (vc[w - 1] || vc[w + 1]){
				vc[w] = 0;
				continue;
			}

			else ans = (ans * 2) % mod;
		}
		if (flag)
			cout << 0 << endl;
		else
			cout << ans << endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 100;
int a[maxn], vis[maxn];
int c[maxn], vc[maxn];
const int mod = 998244353;
int main(){
	int T;
	cin >> T;
	while (T--){
		int n, k;
		cin >> n >> k;
		for (int i = 0; i <= n + 10; i++)
			vis[i] = 0, vc[i] = 0;
		for (int i = 1; i <= n; i++){
			scanf("%d", &a[i]);
			vis[a[i]] = i;
		}
		for (int i = 1; i <= k; i++){
			scanf("%d", &c[i]);
			vc[vis[c[i]]] = 1;
		}
		ll ans = 1;
		int flag = 0;
		vc[0] = vc[n + 1] = 1;
		for (int i = 1; i <= k; i++){
			int x = c[i], w = vis[c[i]];
			if (vc[w - 1] && vc[w + 1]){
				flag = 1;
				break;
			}
			else if (w == n&&vc[w-1]){
				flag = 1;
				break;
			}
			else if (w==n) continue;
			else if (w == 1 && vc[w + 1]){
				flag = 1;
				break;
			}
			else if (w==1) continue;
			else if (vc[w - 1] || vc[w + 1]){
				vc[w] = 0;
				continue;
			}

			else ans = (ans * 2) % mod;
		}
		if (flag)
			cout << 0 << endl;
		else
			cout << ans << endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 100;
int a[maxn], vis[maxn];
int c[maxn], vc[maxn];
const int mod = 998244353;
int main(){
	int T;
	cin >> T;
	while (T--){
		int n, k;
		cin >> n >> k;
		for (int i = 0; i <= n + 10; i++)
			vis[i] = 0, vc[i] = 0;
		for (int i = 1; i <= n; i++){
			scanf("%d", &a[i]);
			vis[a[i]] = i;
		}
		for (int i = 1; i <= k; i++){
			scanf("%d", &c[i]);
			vc[vis[c[i]]] = 1;
		}
		ll ans = 1;
		int flag = 0;
		vc[0] = vc[n + 1] = 1;
		for (int i = 1; i <= k; i++){
			int x = c[i], w = vis[c[i]];
			if (vc[w - 1] && vc[w + 1]){
				flag = 1;
				break;
			}
			else if (w == n&&vc[w-1]){
				flag = 1;
				break;
			}
			else if (w==n) continue;
			else if (w == 1 && vc[w + 1]){
				flag = 1;
				break;
			}
			else if (w==1) continue;
			else if (vc[w - 1] || vc[w + 1]){
				vc[w] = 0;
				continue;
			}

			else ans = (ans * 2) % mod;
		}
		if (flag)
			cout << 0 << endl;
		else
			cout << ans << endl;
	}
	return 0;
}


#include<bits/stdc++.h>
typedef unsigned long long ull;
const double PI = acos(-1.0);
const double eps = 1e-6;
#define cinI(a) scanf("%d",&a)
#define cinII(a,b) scanf("%d%d",&a,&b)
#define cinIII(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define mst(a,k) memset(a,k,sizeof(a))
#define cinS(a) scanf("%s",a)
#define ll long long
#define fi first
#define se second
#define pii pair<int,int>
#define rint register int
inline int readin()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
inline void qwrite(ll x)
{
	if (x < 0) x = ~x + 1, putchar('-');
	if (x > 9) qwrite(x / 10);
	putchar(x % 10 + '0');
}
using namespace std;
const int N=2e5+10;
int a[N],b[N],ans[N],a2[N];
bool vis[N],need[N];
const int mod=998244353;
int n,k;
int main()
{
    rint _=readin();
    while(_--)
    {
        n=readin();
        k=readin();
        for(rint i=1;i<=n;i++) a[i]=readin(),a2[a[i]]=i;
        for(rint i=1;i<=k;i++) need[ b[i]=readin() ]=1;

        for(rint i=1,pos,num;i<=k;i++)
        {
            pos=a2[b[i]];
            num=0;
            if(pos-1>0&&!need[a[pos-1]]) num++;
            if(pos+1<=n&&!need[a[pos+1]]) num++;
            need[b[i]]=0;
            ans[i]=num;
        }
        ll temp=1;

        for(rint i=1;i<=k;i++) temp=(temp*ans[i])%mod;
        //printf("ans=");
        qwrite(temp);
        puts("");

        for(rint i=0;i<=n+1;i++) need[i]=0,vis[i]=0;
    }
    return 0;
}


#include<bits/stdc++.h>
typedef unsigned long long ull;
const double PI = acos(-1.0);
const double eps = 1e-6;
#define cinI(a) scanf("%d",&a)
#define cinII(a,b) scanf("%d%d",&a,&b)
#define cinIII(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define mst(a,k) memset(a,k,sizeof(a))
#define cinS(a) scanf("%s",a)
#define ll long long
#define fi first
#define se second
#define pii pair<int,int>
#define rint register int
inline int readin()
{
    int x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
inline void qwrite(ll x)
{
	if (x < 0) x = ~x + 1, putchar('-');
	if (x > 9) qwrite(x / 10);
	putchar(x % 10 + '0');
}
using namespace std;
const int N=2e5+10;
int a[N],b[N],ans[N],a2[N];
bool vis[N],need[N];
const int mod=998244353;
int n,k;
int main()
{
    rint _=readin();
    while(_--)
    {
        n=readin();
        k=readin();
        for(rint i=1;i<=n;i++) a[i]=readin(),a2[a[i]]=i;
        for(rint i=1;i<=k;i++) need[ b[i]=readin() ]=1;

        for(rint i=1,pos,num;i<=k;i++)
        {
            pos=a2[b[i]];
            num=0;
            if(pos-1>0&&!need[a[pos-1]]) num++;
            if(pos+1<=n&&!need[a[pos+1]]) num++;
            need[pos]=0;
            ans[i]=num;
        }
        ll temp=1;

        for(rint i=1;i<=k;i++) temp=(temp*ans[i])%mod;
        //printf("ans=");
        qwrite(temp);
        puts("");

        for(rint i=0;i<=n+1;i++) need[i]=0,vis[i]=0;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> pii;
const int N = 2e5+5;
const int M = 1e6+5;
const LL Mod = 998244353;
#define pi acos(-1)
#define INF 1e18
#define CT0 cin.tie(0),cout.tie(0)
#define IO ios::sync_with_stdio(false)
#define dbg(ax) cout << "now this num is " << ax << endl;
namespace FASTIO{
    inline int read()
    {
        int x = 0,f = 1;char c = getchar();
        while(c < '0' || c > '9'){if(c == '-')f = -1;c = getchar();}
        while(c >= '0' && c <= '9'){x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}
        return x * f;
    }
}
using namespace FASTIO;

int a[N],b[N],pos[N];
bool vis[N];
int main()
{
    int ca;ca = read();
    while(ca--)
    {
        memset(vis,0,sizeof(vis));
        int n,k;n = read(),k = read();
        for(int i = 1;i <= n;++i) a[i] = read(),pos[a[i]] = i;
        for(int i = 1;i <= k;++i) b[i] = read(),vis[pos[b[i]]] = 1;
        vis[0] = 1,vis[n + 1] = 1;
        LL ans = 1;
        for(int i = 1;i <= k;++i)
        {
            int ma = pos[b[i]];
            if(vis[ma - 1] == 0 && vis[ma + 1] == 0) ans = ans * 2 % Mod;
            else if(vis[ma - 1] == 0 || vis[ma + 1] == 0) ans = ans * 1;
            else{ans = 0;break;}
            vis[ma] = 0;
        }
        printf("%lld\n",ans);
    }
//    system("pause");
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int long long

const int maxn = 2e5 + 7, mod = 998244353;

int n, k, a[maxn], b[maxn], pos[maxn], ans;
set<int> s, tmp;

void solve() {
	cin >> n >> k;
	vector<int> pos(n + 1); ans = 1;
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
		pos[a[i]] = i;
	}
	for (int i = 0; i < k; ++i) {
		cin >> b[i];
	}
	s = tmp;
	for (int i = k - 1; i >= 0; --i) {
		int x = 2;
		if ((pos[b[i]] < n - 1 && s.count(a[pos[b[i]] + 1])) || (pos[b[i]] == n - 1)) {
			x--;
		}
		if ((pos[b[i]] > 0 && s.count(a[pos[b[i]] - 1])) || (!pos[b[i]])) {
			x--;
		}
		ans *= x, ans %= mod;
		s.insert(b[i]);
	}
	cout << ans << '\n';
}

int32_t main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t; cin >> t; while (t--) solve();
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int long long

const int maxn = 2e5 + 7, mod = 998244353;

int n, k, a[maxn], b[maxn], pos[maxn], ans;
set<int> s, tmp;

void solve() {
	cin >> n >> k; memset(pos, 0, sizeof(pos)); ans = 1;
	for (int i = 0; i < n; ++i) {
		cin >> a[i];
		pos[a[i]] = i;
	}
	for (int i = 0; i < k; ++i) {
		cin >> b[i];
	}
	s = tmp;
	for (int i = k - 1; i >= 0; --i) {
		int x = 2;
		if ((pos[b[i]] < n - 1 && s.count(a[pos[b[i]] + 1])) || (pos[b[i]] == n - 1)) {
			x--;
		}
		if ((pos[b[i]] > 0 && s.count(a[pos[b[i]] - 1])) || (!pos[b[i]])) {
			x--;
		}
		ans *= x, ans %= mod;
		s.insert(b[i]);
	}
	cout << ans << '\n';
}

int32_t main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t; cin >> t; while (t--) solve();
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

const int N=200009;
const int mod=998244353;
int t,n,m,a[N],b[N];
int l[N],r[N],vis[N],pos[N];
int ans=1;
int main(){
	cin>>t;
	while(t--){
		scanf("%d%d",&n,&m);
		ans=1;
		for(int i=1;i<=n;i++){
			scanf("%d",&a[i]);
			pos[a[i]]=i;
			vis[i]=0;
			l[i]=i+1;
			r[i]=i-1;
		}
		for(int i=1;i<=m;i++){
			scanf("%d",&b[i]);
			vis[pos[b[i]]]=1;
		}
		vis[0]=vis[n+1]=1;
		for(int i=1;i<=m;i++){
			int x=0;
			if(!vis[l[pos[b[i]]]])x++;
			if(!vis[r[pos[b[i]]]])x++;
			ans=(ans*x)%mod;//cout<<t<<"-------------"<<i<<" "<<x<<endl;
			if(ans==0)break;
			r[l[pos[b[i]]]]=r[pos[b[i]]];
			l[r[pos[b[i]]]]=l[pos[b[i]]];
		}
		if(m>=n){printf("0\n");continue;}
		printf("%d\n",ans);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

const int md = 998244353;

int main() {
	int t;
	cin >> t;
	while(t--) {
		int n, k;
		cin >> n >> k;
		vector<int> a(n), b(k);
		map<int, int> mp;
		set<int> st;
		for(int i = 0; i < n; i++) {
			cin >> a[i];
			mp[a[i]] = i;
		}
		for(int i = 0; i < k; i++) {
			cin >> b[i];
			st.insert(b[i]);
		}
		long long ans = 1;
		for(int i = 0; i < k; i++) {
			int pos = mp[b[i]];
			long long cnt = 0;
			if(pos > 0 && st.find(a[pos - 1]) == st.end()) {
				cnt++;
			}
			if(pos < (n - 1) && st.find(a[pos + 1]) == st.end()) {
				cnt++;
			}
			ans = (ans * cnt) % md;
			st.erase(b[i]);
		}
		cout << ans << endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define MOD 998244353
#define MAX 200000
using namespace std;
bool bv[MAX+10];
int a[MAX+10],ai[MAX+10],b[MAX+10];
set<int> st;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int t;
	cin>>t;
	while(t--)
	{
		int n,k;
		cin>>n>>k;
		for(int i=1;i<=n;++i)
			bv[i] = false;
		st.clear();
		for(int i=1;i<=n;++i)
			cin>>a[i], ai[a[i]] = i, st.insert(i);
		for(int i=1;i<=k;++i)
			cin>>b[i], bv[b[i]] = true;
		long long ans = 1;
		for(int i=1;ans&&i<=k;++i)
		{
			set<int>::iterator it = st.find(ai[b[i]]);
			if(it==st.end())
			{
				ans = 0;
				continue;
			}
			int cnt = 0,x;
			if(it!=st.begin())
			{
				--it;
				if(!bv[a[*it]])
					cnt++, x = *it;
				++it;
			}
			if(++it!=st.end())
			{
				if(!bv[a[*it]])
					cnt++, x = *it;
				--it;
			}
			if(cnt)
			{
				st.erase(st.find(x));
				ans *= 1LL*cnt;
				ans %= MOD;
				bv[b[i]] = false;
			}
			else ans = 0;
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
#define MOD 998244353
#define MAX 200000
using namespace std;
bool bv[MAX+10];
int a[MAX+10],ai[MAX+10],b[MAX+10];
set<int> st;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	int t;
	cin>>t;
	while(t--)
	{
		int n,k;
		cin>>n>>k;
		for(int i=1;i<=n;++i)
			bv[i] = false;
		st.clear();
		for(int i=1;i<=n;++i)
			cin>>a[i], ai[a[i]] = i, st.insert(i);
		for(int i=1;i<=k;++i)
			cin>>b[i], bv[b[i]] = true;
		long long ans = 1;
		for(int i=1;ans&&i<=k;++i)
		{
			set<int>::iterator x,it = st.find(ai[b[i]]);
			int cnt = 0;
			if(it!=st.begin())
			{
				--it;
				if(!bv[a[*it]])
					cnt++, x = it;
				++it;
			}
			if(++it!=st.end())
			{
				if(!bv[a[*it]])
					cnt++, x = it;
				--it;
			}
			if(cnt)
			{
				st.erase(x);
				ans *= 1LL*cnt;
				ans %= MOD;
				bv[b[i]] = false;
			}
			else ans = 0;
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define X first
#define Y second
#define pb push_back
#define pll pair<ll, ll>
#define pli pair<ll, int>
#define pii pair<int,int>
#define New_Time srand((unsigned)time(NULL))
inline ll gcd(ll a, ll b) { while (b != 0) { ll c = a % b; a = b; b = c; }return a < 0 ? -a : a; }
inline ll lowbit(ll x) { return x & (-x); }
//int head[2000010], Edge_Num;
//struct Edge { int to, next; ll w; }e[4000010];
//inline void ade(int x, int y, ll w) { e[++Edge_Num] = { y,head[x],w }; head[x] = Edge_Num; }
//inline void G_init(int n) { memset(head, 0, sizeof(int) * (n + 100)); Edge_Num = 0; }
int dir[8][2] = { {-1,0},{0,-1},{-1,-1},{1,-1},{1,0},{0,1},{1,1},{-1,1} };
const long double PI = 3.14159265358979323846;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
inline ll rd() {
	ll x = 0; bool f = 1; char ch = getchar();
	while (ch < '0' || ch>'9') { if (ch == '-')f = 0; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar(); }
	return f ? x : -x;
}
const double eps = 1e-8;
const ll mod = 998244353;
const int M = 1e6 + 10;
const int N = 1e6 + 10;
int pos[N];
int a[N], b[N];
int vis[N];
void solve() {
	int n = rd(), k = rd();
	memset(vis, 0, sizeof(bool) * (n + 10));
	memset(pos, 0, sizeof(int) * (n + 10));
	for (int i = 1; i <= n; i++) {
		a[i] = rd();
		pos[a[i]] = i;
	}
	for (int i = 1; i <= k; i++) {
		b[i] = rd();
		vis[b[i]] = i;
	}
	ll ans = 1;
	for (int i = 1; i <= k; i++) {
		int x = b[i];
		int cnt = 0;
		if (pos[x] - 1 >= 1 && vis[a[pos[x] - 1]] < vis[x])++cnt;
		if (pos[x] + 1 <= n && vis[a[pos[x] + 1]] < vis[x])++cnt;
		ans = ans * cnt % mod;
	}
	cout << ans << endl;
}
int main() {
	int _T = 1;
	_T = rd();
	while (_T--)solve();
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define X first
#define Y second
#define pb push_back
#define pll pair<ll, ll>
#define pli pair<ll, int>
#define pii pair<int,int>
#define New_Time srand((unsigned)time(NULL))
inline ll gcd(ll a, ll b) { while (b != 0) { ll c = a % b; a = b; b = c; }return a < 0 ? -a : a; }
inline ll lowbit(ll x) { return x & (-x); }
//int head[2000010], Edge_Num;
//struct Edge { int to, next; ll w; }e[4000010];
//inline void ade(int x, int y, ll w) { e[++Edge_Num] = { y,head[x],w }; head[x] = Edge_Num; }
//inline void G_init(int n) { memset(head, 0, sizeof(int) * (n + 100)); Edge_Num = 0; }
int dir[8][2] = { {-1,0},{0,-1},{-1,-1},{1,-1},{1,0},{0,1},{1,1},{-1,1} };
const long double PI = 3.14159265358979323846;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
inline ll rd() {
	ll x = 0; bool f = 1; char ch = getchar();
	while (ch < '0' || ch>'9') { if (ch == '-')f = 0; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar(); }
	return f ? x : -x;
}
const double eps = 1e-8;
const ll mod = 998244353;
const int M = 1e6 + 10;
const int N = 1e6 + 10;
int pos[N];
int a[N], b[N];
int vis[N];
void solve() {
	int n = rd(), k = rd();
	memset(vis, 0, sizeof(bool)*(n+10));
	for (int i = 1; i <= n; i++) {
		a[i] = rd();
		pos[a[i]] = i;
	}
	for (int i = 1; i <= k; i++) {
		b[i] = rd();
		vis[b[i]] = i;
	}
	ll ans = 1;
	for (int i = 1; i <= k; i++) {
		int x = b[i];
		int cnt = 0;
		if (pos[x] - 1 >= 1 && vis[a[pos[x] - 1]] < vis[x])++cnt;
		if (pos[x] + 1 <= n && vis[a[pos[x] + 1]] < vis[x])++cnt;
		ans = ans * cnt % mod;
	}
	cout << ans << endl;
}
int main() {
	int _T = 1;
	_T = rd();
	while (_T--)solve();
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define X first
#define Y second
#define pb push_back
#define pll pair<ll, ll>
#define pli pair<ll, int>
#define pii pair<int,int>
#define New_Time srand((unsigned)time(NULL))
inline ll gcd(ll a, ll b) { while (b != 0) { ll c = a % b; a = b; b = c; }return a < 0 ? -a : a; }
inline ll lowbit(ll x) { return x & (-x); }
//int head[2000010], Edge_Num;
//struct Edge { int to, next; ll w; }e[4000010];
//inline void ade(int x, int y, ll w) { e[++Edge_Num] = { y,head[x],w }; head[x] = Edge_Num; }
//inline void G_init(int n) { memset(head, 0, sizeof(int) * (n + 100)); Edge_Num = 0; }
int dir[8][2] = { {-1,0},{0,-1},{-1,-1},{1,-1},{1,0},{0,1},{1,1},{-1,1} };
const long double PI = 3.14159265358979323846;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
inline ll rd() {
	ll x = 0; bool f = 1; char ch = getchar();
	while (ch < '0' || ch>'9') { if (ch == '-')f = 0; ch = getchar(); }
	while (ch >= '0' && ch <= '9') { x = (x << 3) + (x << 1) + (ch ^ 48); ch = getchar(); }
	return f ? x : -x;
}
const double eps = 1e-8;
const ll mod = 998244353;
const int M = 1e6 + 10;
const int N = 1e6 + 10;
int pos[N];
int a[N], b[N];
int vis[N];
void solve() {
	int n = rd(), k = rd();
	memset(vis, 0, sizeof(int) * (n + 10));
	memset(pos, 0, sizeof(int) * (n + 10));
	for (int i = 1; i <= n; i++) {
		a[i] = rd();
		pos[a[i]] = i;
	}
	for (int i = 1; i <= k; i++) {
		b[i] = rd();
		vis[b[i]] = i;
	}
	ll ans = 1;
	for (int i = 1; i <= k; i++) {
		int x = b[i];
		int cnt = 0;
		if (pos[x] - 1 >= 1 && vis[a[pos[x] - 1]] < vis[x])++cnt;
		if (pos[x] + 1 <= n && vis[a[pos[x] + 1]] < vis[x])++cnt;
		ans = ans * cnt % mod;
	}
	cout << ans << endl;
}
int main() {
	int _T = 1;
	_T = rd();
	while (_T--)solve();
}


#include<cstdio>
#include<set>
using namespace std;
const int MaxN = 2e5;
const int mod = 998244353;
int b[MaxN + 5], id[MaxN + 5];
int pre[MaxN + 5], suf[MaxN + 5];
bool vis[MaxN + 5];
set<int> st;
set<int>::iterator it1, it2;

int main() {
	int T; scanf("%d", &T);
	while(T--) {
		int n, m;
		scanf("%d %d", &n, &m);
		for(int i = 1; i <= n; ++i) {
			int x; scanf("%d", &x);
			id[x] = i;
			vis[i] = false;
		}
		st.clear();
		for(int i = 1; i <= m; ++i) {
			scanf("%d", &b[i]);
			b[i] = id[b[i]];
			vis[b[i]] = true;
			st.insert(b[i]);
		}
		pre[0] = suf[n + 1] = 0;
		for(int i = 1; i <= n; ++i) pre[i] = pre[i - 1] + !vis[i];
		for(int i = n; i >= 1; --i) suf[i] = suf[i + 1] + !vis[i];;
		int ans = 1, now;
		for(int i = 1; i <= m; ++i) {
			now = 0;
			it1 = st.lower_bound(b[i]);
			if(it1 != st.begin()) {
				it2 = it1;
				--it2;
				if(pre[*it2] != pre[b[i]]) ++now;
			}
			else if(pre[b[i]]) ++now;
			++it1;
			if(it1 != st.end() && suf[*it1] != suf[b[i]]) ++now;
			else if(it1 == st.end() && suf[b[i]]) ++now;
			st.erase(b[i]);
			ans = ans * now % mod;
		}
		printf("%d\n", ans);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

const int maxn = 2e5 + 5;
const int mod = 998244353;
int a[maxn], b[maxn], vis[maxn];

int main(){
	int T;
	cin >> T;
	while(T--){
		int n, m;
		cin >> n >> m;

		int cnt;
		for(int i = 1 ; i <= n ; i++){
			cin >> cnt;
			a[cnt] = i;
			vis[i] = 0;
		}
		int ans = 1, res;
		for(int i = 1 ; i <= m ; i++)cin >> b[i];
		for(int i = m ; i >= 1 ; i--){
			cnt = b[i];

			res = 0;
			if(a[cnt] > 1 && vis[a[cnt] - 1] == 0)res++;
			if(a[cnt] < n && vis[a[cnt] + 1] == 0)res++;

			vis[a[cnt]] = 1;
			ans = (ans * res) % mod;
		}
		cout << ans << endl;
	}
}


#include<cstdio>
#include<set>
using namespace std;
const int MaxN = 2e5;
const int mod = 998244353;
int b[MaxN + 5], id[MaxN + 5];
int pre[MaxN + 5], suf[MaxN + 5];
bool vis[MaxN + 5];
set<int> st;
set<int>::iterator it1, it2;

int main() {
	int T; scanf("%d", &T);
	while(T--) {
		int n, m;
		scanf("%d %d", &n, &m);
		for(int i = 1; i <= n; ++i) {
			int x; scanf("%d", &x);
			id[x] = i;
			vis[i] = false;
		}
		st.clear();
		for(int i = 1; i <= m; ++i) {
			scanf("%d", &b[i]);
			b[i] = id[b[i]];
			vis[b[i]] = true;
			st.insert(b[i]);
		}
		pre[0] = suf[n + 1] = 0;
		for(int i = 1; i <= n; ++i) pre[i] = pre[i - 1] + !vis[i];
		for(int i = n; i >= 1; --i) suf[i] = suf[i + 1] + !vis[i];;
		int ans = 1, now;
		for(int i = 1; i < m; ++i) {
			now = 0;
			it1 = st.lower_bound(b[i]);
			if(it1 != st.begin()) {
				it2 = it1;
				--it2;
				if(pre[*it2] != pre[b[i]]) ++now;
			}
			else if(pre[b[i]]) ++now;
			++it1;
			if(it1 != st.end() && suf[*it1] != suf[b[i]]) ++now;
			else if(it1 == st.end() && suf[b[i]]) ++now;
			st.erase(b[i]);
			ans = ans * now % mod;
		}
		printf("%d\n", ans);
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

typedef long long ll;

const ll mod=998244353;

int arr[200010],brr[200010];
int vis[200010],pos[200010];

int main() {
    int T;
    scanf("%d",&T);
    while(T--) {
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++) {
            scanf("%d",&arr[i]);
            pos[arr[i]]=i;
            vis[i]=0;
        }
        for(int i=1;i<=m;i++) {
            scanf("%d",&brr[i]);
            vis[brr[i]]=1;
        }
        ll ans=1;
        for(int i=1;i<=m;i++) {
            int ti=pos[brr[i]];
            int num=0;
            if(ti-1>=1&&!vis[arr[ti-1]]) num++;
            if(ti+1<=n&&!vis[arr[ti+1]]) num++;
            ans*=num;
            vis[brr[i]]=0;
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

typedef long long ll;

const ll mod=998244353;

int arr[200010],brr[200010];
int vis[200010],pos[200010];

int main() {
    int T;
    scanf("%d",&T);
    while(T--) {
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++) {
            scanf("%d",&arr[i]);
            pos[arr[i]]=i;
            vis[i]=0;
        }
        for(int i=1;i<=m;i++) {
            scanf("%d",&brr[i]);
            vis[brr[i]]=1;
        }
        ll ans=1;
        for(int i=1;i<=m;i++) {
            int ti=pos[brr[i]];
            int num=0;
            if(ti-1>=1&&!vis[arr[ti-1]]) num++;
            if(ti+1<=n&&!vis[arr[ti+1]]) num++;
            ans*=num;
            ans%=mod;
            vis[brr[i]]=0;
        }
        printf("%lld\n",ans);
    }
    return 0;
}


#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <unordered_map>
#include <vector>
#include <algorithm>

using namespace std;
int T, n, m;
typedef long long LL;
const int MOD = 998244353, N = 2e5 + 9;
LL a[N], b[N];
vector<pair<int, int> >vec;
unordered_map<int ,int>getpos;

int main(){
    cin >> T;
    while(T--){
        unordered_map<int, bool>mp;
        vec.clear();
        mp.clear();
        getpos.clear();
        scanf("%d%d", &n, &m);
        for(int i = 1; i <= n; i++){
            scanf("%lld", &a[i]);
            getpos[a[i]] = i;
        }
        for(int i = 1; i <= m; i++){
            scanf("%lld", &b[i]);
            mp[b[i]] = true;
        }
        LL ans = 1;
        for(int i = 1; i <= m; i++){
            int pos = getpos[b[i]];
            LL k = 0;
            if(!mp[a[pos - 1]] && pos - 1 >= 1)k++;
            if(!mp[a[pos + 1]] && pos + 1 <= n)k++;
            ans = ans * k % MOD;
            mp[b[i]] = false;
        }
        printf("%lld\n", ans);
    }
    return 0;
}