#include <bits/stdc++.h>
using namespace std;
long long int power(long long int x, long long int y) {
  long long int res = 1;
  while (y > 0) {
    if (y & 1) {
      res = res * x;
    }
    y = y >> 1;
    x = x * x;
    x %= 1000000007;
    res %= 1000000007;
  }
  return res;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    long long int n, p, i, j, x, y;
    cin >> n >> p;
    vector<long long int> num;
    for (i = 0; i < n; i++) {
      cin >> x;
      num.push_back(x);
    }
    if (p == 1) {
      cout << n % 2 << "\n";
      continue;
    }
    long long int sum = 0, cur = 0, fl = 0, k = 0;
    x = 1000000007;
    sort(num.begin(), num.end(), greater<long long int>());
    for (i = 0; i < n; i++) {
      long long int dif = x - num[i];
      x = num[i];
      sum = sum * power(p, dif) % 1000000007;
      for (j = 0; j < min(20LL, dif); j++) {
        cur *= p;
        if (cur > 1e6 + 100) {
          fl = 1;
          break;
        }
      }
      while (k < n && num[k] == x) {
        k++;
        if (fl || cur > 0) {
          cur--;
          sum--;
          sum += 1000000007;
        } else {
          cur++;
          sum++;
        }
        sum %= 1000000007;
      }
    }
    sum = sum * power(p, x);
    cout << sum % 1000000007 << "\n";
  }
  return 0;
}
