#include <stdio.h>

long long power_mod (long long a, long long b, long long mod_num) {
  long long ans = 1LL;
  
  if (b > 0LL) {
    ans = power_mod(a, b/2LL, mod_num);
    ans = (ans * ans) % mod_num;
    if (b%2LL == 1LL) {
      ans = (ans * (a % mod_num)) % mod_num;
    }
  }
  
  return ans;
}

int main () {
  int t = 0;
  int n = 0;
  long long m = 0LL;
  int x[200000] = {};
  
  int res = 0;
  
  long long mod_num = 998244353LL;
  
  long long pow[200001] = {};
  int cnt[200001][2] = {};
  long long sum[200001][2] = {};
  
  res = scanf("%d", &t);
  while (t > 0) {
    long long ans = 0LL;
    res = scanf("%d", &n);
    res = scanf("%lld", &m);
    for (int i = 0; i < n; i++) {
      res = scanf("%d", x+i);
    }
    for (int i = 0; i < n; i++) {
      cnt[i+1][0] = cnt[i][0];
      sum[i+1][0] = sum[i][0];
      if (x[i] < 0) {
        cnt[i+1][0]++;
      } else {
        sum[i+1][0] += (long long)(x[i]);
      }
    }
    if (sum[n][0]%m != 0LL && cnt[n][0] <= 0) {
      printf("0\n");
    } else if (cnt[n][0] <= 0) {
      ans = (long long)(n-1);
      for (int i = 0; i < n-1; i++) {
        if (sum[i+1][0]%m == 0LL) {
          ans -= 1LL;
        }
      }
      printf("%lld\n", ans%mod_num);
    } else {
      ans = power_mod(m, (long long)(cnt[n][0]-1), mod_num);
      ans *= (long long)(n-1);
      ans %= mod_num;
      cnt[n][1] = 0;
      sum[n][1] = 0LL;
      for (int i = n-1; i >= 0; i--) {
        cnt[i][1] = cnt[i+1][1];
        sum[i][1] = sum[i+1][1];
        if (x[i] < 0) {
          cnt[i][1]++;
        } else {
          sum[i][1] += (long long)x[i];
        }
      }
      for (int i = 0; i < n-1; i++) {
        if (cnt[i+1][0] <= 0 && sum[i+1][0]%m == 0LL) {
          ans += mod_num-power_mod(m, (long long)(cnt[i][1]-1), mod_num);
        } else if (cnt[i+1][1] <= 0 && sum[i+1][1]%m == 0LL) {
          ans += mod_num-power_mod(m, (long long)(cnt[i+1][0]-1), mod_num);
        } else if (cnt[i+1][0] > 0 && cnt[i+1][1] > 0) {
          ans += mod_num-power_mod(m, (long long)(cnt[i+1][0]+cnt[i+1][1]-2), mod_num);
        }
      }
      printf("%lld\n", ans%mod_num);
    }
    t--;
  }
  
  return 0;
}