Johnny and Grandmaster
Contest: Codeforces Round 647 (Div. 1) - Thanks, Algo Muse!
Difficulty rating: 1900
Tags: greedy, implementation, math, sortings

Johnny has just found the new, great tutorial: "How to become a grandmaster?". The tutorial tells many strange and unexpected for Johnny things, such as you have to be patient or that very important is solving many harder and harder problems.

The boy has found an online judge with tasks divided by topics they cover. He has picked $$$p^{k_i}$$$ problems from $$$i$$$-th category ($$$p$$$ is his favorite number). He wants to solve them in two weeks (the patience condition is too hard for Johnny, so for simplicity, he looks only at easy tasks, which can be solved in such a period). Now our future grandmaster has to decide which topics to cover first and which the second week. Help him assign topics in such a way, that workload is balanced.

Formally, given $$$n$$$ numbers $$$p^{k_i}$$$, the boy wants to divide them into two disjoint sets, minimizing the absolute difference between sums of numbers in each set. Find the minimal absolute difference. Output the result modulo $$$10^{9}+7$$$.

Input format:
Input consists of multiple test cases. The first line contains one integer $$$t$$$ $$$(1 \leq t \leq 10^5)$$$ — the number of test cases. Each test case is described as follows:

The first line contains two integers $$$n$$$ and $$$p$$$ $$$(1 \leq n, p \leq 10^6)$$$. The second line contains $$$n$$$ integers $$$k_i$$$ $$$(0 \leq k_i \leq 10^6)$$$.

The sum of $$$n$$$ over all test cases doesn't exceed $$$10^6$$$.

Output format:
Output one integer — the reminder of division the answer by $$$1\,000\,000\,007$$$.

Constraints:
Time limit: 2.0 s
Memory limit: 256.0 MB

Note:
You have to minimize the difference, not it's remainder. For example, if the minimum difference is equal to $$$2$$$, but there is also a distribution where the difference is $$$10^9 + 8$$$, then the answer is $$$2$$$, not $$$1$$$.

In the first test case of the example, there're the following numbers: $$$4$$$, $$$8$$$, $$$16$$$, $$$16$$$, and $$$8$$$. We can divide them into such two sets: $$${4, 8, 16}$$$ and $$${8, 16}$$$. Then the difference between the sums of numbers in sets would be $$$4$$$.

Example input:
4
5 2
2 3 4 4 3
3 1
2 10 1000
4 5
0 1 1 100
1 8
89

Example output:
4
1
146981438
747093407

Editorial:
The solution for the case $$$p = 1$$$ is trivial, the answer is $$$1$$$ for odd $$$n$$$ and $$$0$$$ for even $$$n$$$. From now on, I will assume that $$$p > 1$$$. Instead of partitioning the elements into two sets, I will think of placing plus and minus signs before them to minimize the absolute value of the resulting expression.
We will process the exponents in non-increasing order and maintain the invariant that the current sum is nonnegative. Say we are processing $$$k_i.$$$ In such cases, we will know the current sum modulo $$$10^{9}+7$$$ and its exact value divided by $$$p^{k_i}$$$ (denoted as $$$v$$$) or information that it's too big.
Initially, the sum (I will denote it $$$s$$$) equals $$$0$$$. While processing elements:
If $$$s > 0$$$, subtract the current element from the sum (it easy to show that it won't be negative after this operation).
If $$$s = 0$$$, add the current element to the sum.
If at any point of the algorithm, $$$v = \frac{s}{p^{k_i}} > n$$$, there is no need to store the exact value of $$$v$$$ anymore, because it is so big that all the elements from this moment on will be subtracted. Thus, it is enough to store this information and the current sum modulo $$$10^{9}+7$$$.
When we move to the next element, the exponent may change, and $$$v$$$ needs to be multiplied by a power of $$$p$$$. Since the exponents can be large, we use fast multiplication.
The time complexity of this solution is $$$\mathcal{O}(n \log n + n \log \max k_i)$$$.