import sys
input = sys.stdin.readline
for _ in range(int(input())):
n, p = map(int, input().split());s = list(map(int, input().split()));s.sort(reverse=True)
if p == 1:print(n % 2);continue
c = -1;ci = 0
for i, si in enumerate(s):
if c == -1:c = si;ls = si;f = 1;ci = i
else:
d = ls - si
if d > 20:break
elif d:f *= p ** d
ls = si;f -= 1
if f > n:break
if f == 0:c = -1
if c != -1:
ans = pow(p, c, 1000000007)
for si in s[ci+1:]:ans -= pow(p, si, 1000000007)
ans %= 1000000007;ans += 1000000007
else:ans = 0
print(ans % 1000000007)
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1000000;
long long mod = 1000000007;
long long ksm(long long p, long long x) {
long long res = 1;
while(x) {
if(x & 1) {
res = (res * p) % mod;
}
p = (p * p) % mod;
x >>= 1;
}
return res;
}
struct asd{
int v, c;
}e[N];
int main() {
int t, i, n, p, a[N];
bool flag;
int tar, k, l;
long long res;
for(cin >> t; t; t--) {
cin >> n >> p;
flag = true;
l = 0;
for(i = 0; i < n; i++) cin >> a[i];
if(p == 1) {
cout << n % 2 << endl;
continue;
}
sort(a, a + n);
tar = a[n - 1];
for(i = n - 2; i > -1; i--) {
k = a[i];
while(l && k == e[l - 1].v && e[l - 1].c == p - 1) {
l--;
k++;
}
if(k == tar) {
if(i == 0) {
cout << 0 << endl;
flag = false;
l = 0;
break;
}
i--;
tar = a[i];
} else if(l && k == e[l - 1].v) {
e[l - 1].c++;
} else {
e[l].c = 1;
e[l].v = k;
l++;
}
}
res = ksm(p, tar);
for(i = 0; i < l; i++) {
res -= e[i].c * ksm(p, e[i].v);
res %= mod;
}
res = (res + mod) % mod;
if(flag) cout << res << endl;
}
return 0;
}
779. K-th Symbol in Grammar | 701. Insert into a Binary Search Tree |
429. N-ary Tree Level Order Traversal | 739. Daily Temperatures |
647. Palindromic Substrings | 583. Delete Operation for Two Strings |
518. Coin Change 2 | 516. Longest Palindromic Subsequence |
468. Validate IP Address | 450. Delete Node in a BST |
445. Add Two Numbers II | 442. Find All Duplicates in an Array |
437. Path Sum III | 436. Find Right Interval |
435. Non-overlapping Intervals | 406. Queue Reconstruction by Height |
380. Insert Delete GetRandom O(1) | 332. Reconstruct Itinerary |
368. Largest Divisible Subset | 377. Combination Sum IV |
322. Coin Change | 307. Range Sum Query - Mutable |
287. Find the Duplicate Number | 279. Perfect Squares |
275. H-Index II | 274. H-Index |
260. Single Number III | 240. Search a 2D Matrix II |
238. Product of Array Except Self | 229. Majority Element II |