#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
using namespace std;
mt19937 rnd(239);
const int N = 1000001;
long long mod = 1000000007;
long long k[N];
long long bp(long long x, long long st) {
  long long res = 1;
  for (long long t = x; st > 0; st >>= 1, t = t * t % mod)
    if (st & 1ll) res = res * t % mod;
  return res;
}
void add(long long &x, long long t) {
  if (t < 0)
    x += mod + t;
  else
    x += t;
  if (x >= mod) x -= mod;
}
void solve() {
  long long n, p;
  cin >> n >> p;
  long long ost = 0;
  long long mxk = 0;
  for (int i = 0; i < n; ++i) {
    cin >> k[i];
  }
  if (p == 1) {
    cout << n % 2 << "\n";
    return;
  }
  sort(k, k + n);
  k[n] = k[n - 1];
  long long res = 0;
  long long ans = 0;
  for (long long i = n - 1; i >= 0; --i) {
    if (res != 0)
      for (long long j = k[i + 1] - k[i]; res < n && j > 0; --j, res *= p)
        ;
    if (res > 0)
      --res, add(ans, -bp(p, k[i]));
    else
      ++res, add(ans, bp(p, k[i]));
  }
  cout << ans << "\n";
}
void pre_calc() {}
int main() {
  pre_calc();
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int tst = 10;
  if (tst != 1) cin >> tst;
  while (tst--) solve();
}
