#include <bits/stdc++.h>
#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3, "Ofast", "inline")
using namespace std;
const int inf = 0x3f3f3f3f;
const long long INF = 0x3f3f3f3f3f3f3f3fLL;
const long long mod = 1e9 + 7;
const int maxn = 1e6 + 100;
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int t;
long long n, p, k[maxn];
long long qpow(long long x, long long y) {
  long long ans = 1, base = x;
  while (y) {
    if (y % 2 == 1) ans = (ans * base) % mod;
    base = (base * base) % mod;
    y >>= 1;
  }
  return ans;
}
bool cmp(long long x, long long y) { return x > y; }
int main() {
  scanf("%d", &t);
  while (t--) {
    scanf("%lld%lld", &n, &p);
    for (int i = 1; i <= n; i++) scanf("%lld", &k[i]);
    if (p == 1) {
      if (n % 2 == 1)
        printf("1\n");
      else
        printf("0\n");
      continue;
    }
    sort(k + 1, k + n + 1, cmp);
    long long num = 0;
    for (int i = 1; i <= n; i++) {
      if (num == 0) {
        num = 1;
        continue;
      }
      num = num * qpow(p, k[i - 1] - k[i]) % mod;
      num = (num - 1 + mod) % mod;
    }
    long long ans = num * qpow(p, k[n]) % mod;
    printf("%lld\n", ans);
  }
  return 0;
}
