#include <bits/stdc++.h>
using namespace std;
void file(string s) {
  freopen((s + ".in").c_str(), "r", stdin);
  freopen((s + ".out").c_str(), "w", stdout);
}
template <typename Tp>
void read(Tp &x) {
  x = 0;
  long long fh = 1;
  char c = getchar();
  while (c > '9' || c < '0') {
    if (c == '-') {
      fh = -1;
    }
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  x *= fh;
}
long long ksm(long long b, long long p) {
  long long ret = 1;
  while (p) {
    if (p & 1) ret = ret * b % 1000000007;
    b = b * b % 1000000007;
    p >>= 1;
  }
  return ret;
}
long long n, m;
long long a[1000005], sum;
signed main() {
  long long T;
  read(T);
  while (T--) {
    read(n);
    read(m);
    sum = 0;
    for (long long i = 1; i <= n; i++) read(a[i]);
    if (m == 1) {
      puts((n & 1) ? "1" : "0");
      continue;
    }
    sort(a + 1, a + n + 1);
    long long cursm = 1;
    sum++;
    for (long long i = n - 1; i; i--) {
      long long dlt = a[i + 1] - a[i];
      sum = sum * ksm(m, dlt) % 1000000007;
      if (cursm) {
        for (long long j = 1; cursm <= n && j <= dlt; j++) {
          cursm *= m;
        }
      }
      if (cursm > 0) {
        cursm--;
        sum = (sum + (1000000007 - 1)) % 1000000007;
      } else {
        cursm++;
        sum++;
        sum %= 1000000007;
      }
    }
    sum = sum * ksm(m, a[1]) % 1000000007;
    printf("%lld\n", sum);
  }
  return 0;
}
