#include <bits/stdc++.h>
#define ff first
#define ss second
#define ll long long
#define ld long double
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define pii pair<int, int>
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define sws ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl '\n'
#define teto(a, b) ((a+b-1)/(b))
using namespace std;

const int MAX = 100010;
const int MOD = 1000000007;
const int INF = 1e8;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const ld EPS = 1e-7;

// Extra
#define forn(i, n) for(int i = 0; i < (int)n; i++)
#define forne(i, a, b) for(int i = a; i <= b; i++)
#define all(x) x.begin(), x.end()
//

ll fexp(ll b, ll e) {
    if(e == 0) return 1;
    ll res = fexp(b, e/2);
    res = (res * res) % MOD;
    if(e%2)
        res = (b*res) % MOD;

    return res;
}

int main() {
    sws;
    ll n, x;
    cin >> n >> x;

    ll soma = 0;
    vl v(n), v2;
    forn(i, n) {
        cin >> v[i];
        soma += v[i];
    }

    forn(i, n) {
        v[i] = soma - v[i];
    }

    sort(all(v));

    map<ll, ll> mp;
    for(int i = 0; i < v.size(); i++) {
        mp[v[i]]++;
        if(mp[v[i]] == x) {
            mp[v[i]] = 0;
            v.pb(v[i]+1);
        }
    }

    for(auto p : mp) {
        if(p.ss != 0) {
            v2.pb(p.ff);
        }
    }
    swap(v, v2);
    sort(all(v));

    // for(auto ex : v) {
    //     cout << ex << " ";
    // }
    // cout << endl;

    cout << fexp(x, v[0]) << endl;

    return 0;
}


#include <bits/stdc++.h>
#define ff first
#define ss second
#define ll long long
#define ld long double
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define pii pair<int, int>
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define sws ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl '\n'
#define teto(a, b) ((a+b-1)/(b))
using namespace std;

const int MAX = 100010;
const int MOD = 1000000007;
const int INF = 1e8;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const ld EPS = 1e-7;

// Extra
#define forn(i, n) for(int i = 0; i < (int)n; i++)
#define forne(i, a, b) for(int i = a; i <= b; i++)
#define all(x) x.begin(), x.end()
//

ll fexp(ll b, ll e) {
    if(e == 0) return 1;
    ll res = fexp(b, e/2);
    res = (res * res) % MOD;
    if(e%2)
        res = (b*res) % MOD;

    return res;
}

int main() {
    sws;
    ll n, x;
    cin >> n >> x;

    ll soma = 0;
    vl v(n), v2;
    forn(i, n) {
        cin >> v[i];
        soma += v[i];
    }

    forn(i, n) {
        v[i] = soma - v[i];
    }

    sort(all(v));
    stack<ll> pil;
    map<ll, ll> mp;

    forn(i, n)
        pil.push(v[i]);

    while(!pil.empty()) {
        ll val = pil.top(); pil.pop();
        mp[val]++;
        if(mp[val] == x) {
            mp[val] = 0;
            pil.push(val+1);
        }
    }

    for(auto p : mp) {
        if(p.ss != 0) {
            v2.pb(p.ff);
        }
    }
    swap(v, v2);
    sort(all(v));

    // for(auto ex : v) {
    //     cout << ex << " ";
    // }
    // cout << endl;

    cout << fexp(x, v[0]) << endl;

    return 0;
}


#include <bits/stdc++.h>
#define ff first
#define ss second
#define ll long long
#define ld long double
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define pii pair<int, int>
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define sws ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl '\n'
#define teto(a, b) ((a+b-1)/(b))
using namespace std;

const int MAX = 100010;
const int MOD = 1000000007;
const int INF = 1e8;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const ld EPS = 1e-7;

// Extra
#define forn(i, n) for(int i = 0; i < (int)n; i++)
#define forne(i, a, b) for(int i = a; i <= b; i++)
#define all(x) x.begin(), x.end()
//

ll fexp(ll b, ll e) {
    if(e == 0) return 1;
    ll res = fexp(b, e/2);
    res = (res * res) % MOD;
    if(e%2)
        res = (b*res) % MOD;

    return res;
}

int main() {
    ll n, x;
    cin >> n >> x;

    ll soma = 0;
    vl v(n), v2;
    forn(i, n) {
        cin >> v[i];
        soma += v[i];
    }

    forn(i, n) {
        v[i] = soma - v[i];
    }

    sort(all(v));

    map<ll, ll> mp;
    for(int i = 0; i < v.size(); i++) {
        mp[v[i]]++;
        if(mp[v[i]] == x) {
            mp[v[i]] = 0;
            v.pb(v[i]+1);
        }
    }

    for(auto p : mp) {
        if(p.ss != 0) {
            v2.pb(p.ff);
        }
    }
    swap(v, v2);
    sort(all(v));

    // for(auto ex : v) {
    //     cout << ex << " ";
    // }
    // cout << endl;

    cout << fexp(x, v[0]) << endl;

    return 0;
}


#include <bits/stdc++.h>

template<typename T> T gcd(T a, T b) {
    if(!b) return a;
    return gcd(b, a % b);
}
template<typename T> T lcm(T a, T b) {
    return a * b / gcd(a, b);
}

template<typename T> void chmin(T& a, T b) { a = (a > b) ? b : a; }
template<typename T> void chmax(T& a, T b) { a = (a < b) ? b : a; }
int in() { int x; scanf("%d", &x); return x; }

using namespace std;

typedef long long Int;
typedef unsigned uint;

const int MAXN = 100005;
const int MOD = 1000000007LL;

int N, X;
int P[MAXN];

Int modpow(Int a, Int n) {
    Int res(1);
    while (n) {
        if (n&1) {
            res=(res*a)%MOD;
        }
        a=(a*1ll*a)%MOD;
        n>>=1;
    }
    return res % MOD;
}

int main(void) {
    cin >> N >> X;

    Int denom_exp = 0;
    Int exponent = 0;

    for (int i = 0; i < N; i++) {
        cin >> P[i];
        denom_exp += P[i];
    }

    for (int i = 0; i < N; i++) {
        P[i] = denom_exp - P[i];
    }

    while (true) {
        Int smallest_power = LLONG_MAX;
        int cnt = 0;

        for (int i = 0; i < N; i++) {
            if (smallest_power > P[i]) {
                smallest_power = P[i];
                cnt = 1;
            } else if (smallest_power == P[i]) {
                cnt += 1;
            }
        }

        exponent += smallest_power;

        if (smallest_power == 0 || cnt % X != 0) break;

        for (int i = 0; i < N; i++) {
            P[i] /= smallest_power;
        }

        for (int i = 0; i < N; i++) {
            if (P[i] == 1) {
                while (cnt % X == 0) {
                    P[i] *= X;
                    cnt /= X;
                }
                break;
            }
        }
    }

    cout << modpow(X, exponent) << "\n";

    return 0;
}


#include <bits/stdc++.h>

template<typename T> T gcd(T a, T b) {
    if(!b) return a;
    return gcd(b, a % b);
}
template<typename T> T lcm(T a, T b) {
    return a * b / gcd(a, b);
}

template<typename T> void chmin(T& a, T b) { a = (a > b) ? b : a; }
template<typename T> void chmax(T& a, T b) { a = (a < b) ? b : a; }
int in() { int x; scanf("%d", &x); return x; }

using namespace std;

typedef long long Int;
typedef unsigned uint;

const int MAXN = 100005;
const int MOD = 1000000007LL;

int N, X;
int P[MAXN];

Int modpow(Int a, Int n) {
    Int res(1);
    while (n) {
        if (n&1) {
            res=(res*a)%MOD;
        }
        a=(a*1ll*a)%MOD;
        n>>=1;
    }
    return res % MOD;
}

int main(void) {
    cin >> N >> X;

    Int denom_exp = 0;
    Int exponent = 0;

    for (int i = 0; i < N; i++) {
        cin >> P[i];
        denom_exp += P[i];
    }

    vector<Int> pw;

    for (int i = 0; i < N; i++) {
        pw.push_back(denom_exp - P[i]);
    }

    sort(pw.rbegin(), pw.rend());

    while (true) {
        Int smallest_power = pw[(int) pw.size() - 1];
        int cnt = 0;

        while (!pw.empty() && pw.back() == smallest_power) {
            cnt += 1;
            pw.pop_back();
        }

        exponent += smallest_power;

        if (cnt == 0 || smallest_power == 0 || cnt % X != 0) break;

        int nw = 1;

        while (cnt % X == 0) {
            cnt /= X;
            nw += 1;
        }
        nw -= 1;
        pw.push_back(nw);

        sort(pw.rbegin(), pw.rend());
    }

    cout << modpow(X, exponent) << "\n";

    return 0;
}


#include <bits/stdc++.h>

template<typename T> T gcd(T a, T b) {
    if(!b) return a;
    return gcd(b, a % b);
}
template<typename T> T lcm(T a, T b) {
    return a * b / gcd(a, b);
}

template<typename T> void chmin(T& a, T b) { a = (a > b) ? b : a; }
template<typename T> void chmax(T& a, T b) { a = (a < b) ? b : a; }
int in() { int x; scanf("%d", &x); return x; }

using namespace std;

typedef long long Int;
typedef unsigned uint;

const int MAXN = 100005;
const int MOD = 1000000007LL;

int N, X;
Int P[MAXN];

Int modpow(Int a, Int n) {
	Int res(1);
	while (n) {
		if (n&1) {
			res=(res*a)%MOD;
		}
		a=(a*1ll*a)%MOD;
		n>>=1;
	}
	return res % MOD;
}

int main(void) {
	cin >> N >> X;

	Int denom_exp = 0;
	Int ans = 0;

	for (int i = 0; i < N; i++) {
		cin >> P[i];
		denom_exp += P[i];
	}

	vector<Int> pw;

	for (int i = 0; i < N; i++) {
		pw.push_back(denom_exp - P[i]);
	}

	sort(pw.rbegin(), pw.rend());

	while (true) {
		Int smallest_power = pw.back();
		int cnt = 0;

		while (!pw.empty() && pw.back() == smallest_power) {
			cnt += 1;
			pw.pop_back();
		}

		ans = smallest_power;

		if (cnt % X != 0) break;

		cnt /= X;

		for (int i = 0; i < cnt; i++) {
			pw.push_back(smallest_power + 1);
		}
	}

	cout << modpow(X, min(ans, denom_exp)) << "\n";

	return 0;
}


#include <bits/stdc++.h>

template<typename T> T gcd(T a, T b) {
    if(!b) return a;
    return gcd(b, a % b);
}
template<typename T> T lcm(T a, T b) {
    return a * b / gcd(a, b);
}

template<typename T> void chmin(T& a, T b) { a = (a > b) ? b : a; }
template<typename T> void chmax(T& a, T b) { a = (a < b) ? b : a; }
int in() { int x; scanf("%d", &x); return x; }

using namespace std;

typedef long long Int;
typedef unsigned uint;

const int MAXN = 100005;
const int MOD = 1000000007LL;

int N, X;
int P[MAXN];

Int modpow(Int a, Int n) {
    Int res(1);
    while (n) {
        if (n&1) {
            res=(res*a)%MOD;
        }
        a=(a*1ll*a)%MOD;
        n>>=1;
    }
    return res % MOD;
}

int main(void) {
    cin >> N >> X;

    Int denom_exp = 0;
    Int numer_value = 0LL;

    for (int i = 0; i < N; i++) {
        cin >> P[i];

        denom_exp += P[i];
    }

    for (int i = 0; i < N; i++) {
        numer_value += modpow(X, denom_exp - P[i]);
        numer_value %= MOD;
    }

    Int denom_value = modpow(X, denom_exp);
    Int val_gcd = gcd(numer_value, denom_value);

    //cout << numer_value << " " << denom_value << endl;

    if (val_gcd == 1) {
        cout << min(numer_value, denom_value) << "\n";
    } else {
        cout << val_gcd << "\n";
    }

    return 0;
}


#include <bits/stdc++.h>

template<typename T> T gcd(T a, T b) {
    if(!b) return a;
    return gcd(b, a % b);
}
template<typename T> T lcm(T a, T b) {
    return a * b / gcd(a, b);
}

template<typename T> void chmin(T& a, T b) { a = (a > b) ? b : a; }
template<typename T> void chmax(T& a, T b) { a = (a < b) ? b : a; }
int in() { int x; scanf("%d", &x); return x; }

using namespace std;

typedef long long Int;
typedef unsigned uint;

const int MAXN = 100005;
const int MOD = 1000000007LL;

int N, X;
Int P[MAXN];

Int modpow(Int a, Int n) {
    Int res(1);
    while (n) {
        if (n&1) {
            res=(res*a)%MOD;
        }
        a=(a*1ll*a)%MOD;
        n>>=1;
    }
    return res % MOD;
}

int main(void) {
    cin >> N >> X;

    Int denom_exp = 0;
    Int exponent = 0;

    for (int i = 0; i < N; i++) {
        cin >> P[i];
        denom_exp += P[i];
    }

    vector<Int> pw;

    for (int i = 0; i < N; i++) {
        pw.push_back(denom_exp - P[i]);
    }

    sort(pw.rbegin(), pw.rend());

    while (true) {
        Int smallest_power = pw[(int) pw.size() - 1];
        int cnt = 0;

        while (!pw.empty() && pw.back() == smallest_power) {
            cnt += 1;
            pw.pop_back();
        }

        exponent += smallest_power;
        if (cnt == 0 || smallest_power == 0 || cnt % X != 0) break;

        while (cnt % X == 0) {
            pw.push_back(1);
            cnt /= X;
        }
    }

    cout << modpow(X, min(exponent, denom_exp)) << "\n";

    return 0;
}


#include <bits/stdc++.h>

template<typename T> T gcd(T a, T b) {
    if(!b) return a;
    return gcd(b, a % b);
}
template<typename T> T lcm(T a, T b) {
    return a * b / gcd(a, b);
}

template<typename T> void chmin(T& a, T b) { a = (a > b) ? b : a; }
template<typename T> void chmax(T& a, T b) { a = (a < b) ? b : a; }
int in() { int x; scanf("%d", &x); return x; }

using namespace std;

typedef long long Int;
typedef unsigned uint;

const int MAXN = 100005;
const int MOD = 1000000007LL;

int N, X;
int P[MAXN];

Int modpow(Int a, Int n) {
    Int res(1);
    while (n) {
        if (n&1) {
            res=(res*a)%MOD;
        }
        a=(a*1ll*a)%MOD;
        n>>=1;
    }
    return res % MOD;
}

int main(void) {
    cin >> N >> X;

    Int denom_exp = 0;
    Int exponent = 0;

    for (int i = 0; i < N; i++) {
        cin >> P[i];
        denom_exp += P[i];
    }

    vector<Int> pw;

    for (int i = 0; i < N; i++) {
        pw.push_back(denom_exp - P[i]);
    }

    sort(pw.rbegin(), pw.rend());

    while (true) {
        Int smallest_power = pw[(int) pw.size() - 1];
        int cnt = 0;

        while (!pw.empty() && pw.back() == smallest_power) {
            cnt += 1;
            pw.pop_back();
        }

        exponent += smallest_power;

        if (cnt == 0 || smallest_power == 0 || cnt % X != 0) break;

        int nw = 1;

        while (cnt % X == 0) {
            cnt /= X;
            nw += 1;
        }
        nw -= 1;
        pw.push_back(nw);

        sort(pw.rbegin(), pw.rend());
    }

    cout << modpow(X, min(exponent, denom_exp)) << "\n";

    return 0;
}


#include <bits/stdc++.h>

template<typename T> T gcd(T a, T b) {
    if(!b) return a;
    return gcd(b, a % b);
}
template<typename T> T lcm(T a, T b) {
    return a * b / gcd(a, b);
}

template<typename T> void chmin(T& a, T b) { a = (a > b) ? b : a; }
template<typename T> void chmax(T& a, T b) { a = (a < b) ? b : a; }
int in() { int x; scanf("%d", &x); return x; }

using namespace std;

typedef long long Int;
typedef unsigned uint;

const int MAXN = 100005;
const int MOD = 1000000007LL;

int N, X;
Int P[MAXN];

Int modpow(Int a, Int n) {
    Int res(1);
    while (n) {
        if (n&1) {
            res=(res*a)%MOD;
        }
        a=(a*1ll*a)%MOD;
        n>>=1;
    }
    return res % MOD;
}

int main(void) {
    cin >> N >> X;

    Int denom_exp = 0;
    Int exponent = 0;

    for (int i = 0; i < N; i++) {
        cin >> P[i];
        denom_exp += P[i];
    }

    vector<Int> pw;

    for (int i = 0; i < N; i++) {
        pw.push_back(denom_exp - P[i]);
    }

    sort(pw.rbegin(), pw.rend());

    while (true) {
        Int smallest_power = pw[(int) pw.size() - 1];
        int cnt = 0;

        while (!pw.empty() && pw.back() == smallest_power) {
            cnt += 1;
            pw.pop_back();
        }

        exponent += smallest_power;
        if (cnt == 0 || smallest_power == 0 || cnt % X != 0) break;

        while (cnt % 2 == 0) {
            pw.push_back(1);
            cnt /= 2;
        }
    }

    cout << modpow(X, min(exponent, denom_exp)) << "\n";

    return 0;
}


#include <bits/stdc++.h>

template<typename T> T gcd(T a, T b) {
    if(!b) return a;
    return gcd(b, a % b);
}
template<typename T> T lcm(T a, T b) {
    return a * b / gcd(a, b);
}

template<typename T> void chmin(T& a, T b) { a = (a > b) ? b : a; }
template<typename T> void chmax(T& a, T b) { a = (a < b) ? b : a; }
int in() { int x; scanf("%d", &x); return x; }

using namespace std;

typedef long long Int;
typedef unsigned uint;

const int MAXN = 100005;
const int MOD = 1000000007LL;

int N, X;
int P[MAXN];

Int modpow(Int a, Int n) {
	Int res(1);
	while (n) {
		if (n&1) {
			res=(res*a)%MOD;
		}
		a=(a*1ll*a)%MOD;
		n>>=1;
	}
	return res % MOD;
}

int main(void) {
	cin >> N >> X;

	Int denom_exp = 0;
	Int exponent = 0;

	for (int i = 0; i < N; i++) {
		cin >> P[i];
		denom_exp += P[i];
	}

	for (int i = 0; i < N; i++) {
		P[i] = denom_exp - P[i];
	}

	while (true) {
		Int smallest_power = LLONG_MAX;
		int cnt = 0;

		for (int i = 0; i < N; i++) {
			if (smallest_power > P[i]) {
				smallest_power = P[i];
				cnt = 1;
			} else if (smallest_power == P[i]) {
				cnt += 1;
			}
		}

		exponent += smallest_power;

		if (smallest_power == 0 || cnt % X != 0) break;

		for (int i = 0; i < N; i++) {
			P[i] /= smallest_power;
		}

		for (int i = 0; i < N; i++) {
			if (P[i] == 1) {
				while (cnt % X == 0) {
					cnt /= X;
					P[i] += 1;
				}
			}
		}
	}

	cout << modpow(X, exponent) << "\n";

	return 0;
}


#include <bits/stdc++.h>

template<typename T> T gcd(T a, T b) {
    if(!b) return a;
    return gcd(b, a % b);
}
template<typename T> T lcm(T a, T b) {
    return a * b / gcd(a, b);
}

template<typename T> void chmin(T& a, T b) { a = (a > b) ? b : a; }
template<typename T> void chmax(T& a, T b) { a = (a < b) ? b : a; }
int in() { int x; scanf("%d", &x); return x; }

using namespace std;

typedef long long Int;
typedef unsigned uint;

const int MAXN = 100005;
const int MOD = 1000000007LL;

int N, X;
Int P[MAXN];

Int modpow(Int a, Int n) {
	Int res(1);
	while (n) {
		if (n&1) {
			res=(res*a)%MOD;
		}
		a=(a*1ll*a)%MOD;
		n>>=1;
	}
	return res % MOD;
}

int main(void) {
	cin >> N >> X;

	Int denom_exp = 0;
	Int exponent = 0;

	for (int i = 0; i < N; i++) {
		cin >> P[i];
		denom_exp += P[i];
	}

	vector<Int> pw;

	for (int i = 0; i < N; i++) {
		pw.push_back(denom_exp - P[i]);
	}

	sort(pw.rbegin(), pw.rend());

	while (true) {
		Int smallest_power = pw[(int) pw.size() - 1];
		int cnt = 0;

		while (!pw.empty() && pw.back() == smallest_power) {
			cnt += 1;
			pw.pop_back();
		}

		exponent += smallest_power;
		if (cnt == 0 || smallest_power == 0 || cnt % X != 0) break;

		while (cnt % X == 0) {
			pw.push_back(1);
			cnt /= 2;
		}
	}

	cout << modpow(X, min(exponent, denom_exp)) << "\n";

	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long int lld;

#define sz                           size()
#define pb                           push_back
#define mp                           make_pair
#define F                            first
#define S                            second
#define fill(a,v)                    memset((a),(v),sizeof (a))
#define INF                          INT_MAX
#define mod 1000000007
#define __sync__		     std::ios::sync_with_stdio(false);
#define all(a)			     a.begin(),a.end()

#define TRACE

#ifdef TRACE
#define trace1(x)                cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;

#else

#define trace1(x)
#define trace2(x, y)
#define trace3(x, y, z)
#define trace4(a, b, c, d)
#define trace5(a, b, c, d, e)
#define trace6(a, b, c, d, e, f)

#endif

#define int lld

lld a[100010];

lld mpow(lld A, lld B)
{
	lld ret = 1;
	while(B)
	{
		if(B&1) ret = (ret*A)%mod;
		A = (A*A)%mod;
		B/=2;
	}
	return ret;
}
#undef int
int main()
#define int lld
{
	int n,x,aa;
	cin>>n>>x;
	lld sum = 0;
	for(int i=0;i<n;i++)
	{
		cin>>aa;
		a[i] = aa;
		sum += aa;
	}
	for(int i=0;i<n;i++)
		a[i] = sum - a[i];
	lld ans = 0,ones = 0;
	sort(a,a+n);
	ones = count(a,a+n,a[0]);
	ans = a[0];
	int i = ones;
	while(1)
	{
		if(ones % x) break;
		ones/=x;
		ans++;
		while(i<n && a[i]-ans == 1) ones++;
	}
	cout<<mpow(x,min(ans,sum))<<"\n";

	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long int lld;

#define sz                           size()
#define pb                           push_back
#define mp                           make_pair
#define F                            first
#define S                            second
#define fill(a,v)                    memset((a),(v),sizeof (a))
#define INF                          INT_MAX
#define mod 1000000007
#define __sync__		     std::ios::sync_with_stdio(false);
#define all(a)			     a.begin(),a.end()

#define TRACE

#ifdef TRACE
#define trace1(x)                cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;

#else

#define trace1(x)
#define trace2(x, y)
#define trace3(x, y, z)
#define trace4(a, b, c, d)
#define trace5(a, b, c, d, e)
#define trace6(a, b, c, d, e, f)

#endif

#define int lld

lld a[100010];

lld mpow(lld A, lld B)
{
	lld ret = 1;
	while(B)
	{
		if(B&1) ret = (ret*A)%mod;
		A = (A*A)%mod;
		B/=2;
	}
	return ret;
}
#undef int
int main()
#define int lld
{
	int n,x,aa;
	cin>>n>>x;
	lld sum = 0;
	for(int i=0;i<n;i++)
	{
		cin>>aa;
		a[i] = aa;
		sum += aa;
	}
	for(int i=0;i<n;i++)
		a[i] = sum - a[i];
	lld ans = 0;
	sort(a,a+n);
	ans = a[0];
	int cnt = count(a,a+n,a[0]);
	int i = cnt;
	while(1)
	{
		int tmp = ans;
		if(cnt%x) break;
		while(cnt % x == 0)
		{
			ans += cnt/x;
			cnt/=x;
		}
		while(i<n && a[i] - tmp == 1) i++,ans++;
	}
	ans = min(ans,sum);
	cout<<mpow(x,ans)<<"\n";
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long int lld;

#define sz                           size()
#define pb                           push_back
#define mp                           make_pair
#define F                            first
#define S                            second
#define fill(a,v)                    memset((a),(v),sizeof (a))
#define INF                          INT_MAX
#define mod 1000000007
#define __sync__		     std::ios::sync_with_stdio(false);
#define all(a)			     a.begin(),a.end()

#define TRACE

#ifdef TRACE
#define trace1(x)                cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;

#else

#define trace1(x)
#define trace2(x, y)
#define trace3(x, y, z)
#define trace4(a, b, c, d)
#define trace5(a, b, c, d, e)
#define trace6(a, b, c, d, e, f)

#endif

lld a[100010];

lld mpow(lld a, lld b)
{
	lld ret = 1;
	while(b)
	{
		if(b&1) ret = (ret*a)%mod;
		a = (a*a)%mod;
		b/=2;
	}
	return ret;
}

int main()
{
	int n,x,aa;
	cin>>n>>x;
	lld sum = 0;
	for(int i=0;i<n;i++)
	{
		cin>>aa;
		a[i] = aa;
		sum += aa;
	}
	for(int i=0;i<n;i++)
		a[i] = sum - a[i];
	lld ans = 0;
	sort(a,a+n);
	ans = a[0];
	lld cnt = count(a,a+n,a[0]);
	while((cnt % x) == 0)
	{
		ans += 1;
		cnt/=x;
	}
	ans = min(sum,ans);
	cout<<mpow(x,ans)<<"\n";
	return 0;

}


#include<bits/stdc++.h>
using namespace std;

typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long int lld;

#define sz                           size()
#define pb                           push_back
#define mp                           make_pair
#define F                            first
#define S                            second
#define fill(a,v)                    memset((a),(v),sizeof (a))
#define INF                          INT_MAX
#define mod 1000000007
#define __sync__		     std::ios::sync_with_stdio(false);
#define all(a)			     a.begin(),a.end()

#define TRACE

#ifdef TRACE
#define trace1(x)                cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;

#else

#define trace1(x)
#define trace2(x, y)
#define trace3(x, y, z)
#define trace4(a, b, c, d)
#define trace5(a, b, c, d, e)
#define trace6(a, b, c, d, e, f)

#endif

#define int lld

lld a[100010];

lld mpow(lld A, lld B)
{
	lld ret = 1;
	while(B)
	{
		if(B&1) ret = (ret*A)%mod;
		A = (A*A)%mod;
		B/=2;
	}
	return ret;
}
#undef int
int main()
#define int lld
{
	int n,x,aa;
	cin>>n>>x;
	lld sum = 0;
	for(int i=0;i<n;i++)
	{
		cin>>aa;
		a[i] = aa;
		sum += aa;
	}
	for(int i=0;i<n;i++)
		a[i] = sum - a[i];
	lld ans = 0,ones = 0;
	sort(a,a+n);
	ones = count(a,a+n,a[0]);
	ans = a[0];
	int i = ones;
	while(1)
	{
		if(ones % x) break;
		ones/=x;
		ans++;
		while(i<n && a[i]-ans == 0) ones++,i++;
	}
	cout<<mpow(x,min(ans,sum))<<"\n";

	return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long int lld;

#define sz                           size()
#define pb                           push_back
#define mp                           make_pair
#define F                            first
#define S                            second
#define fill(a,v)                    memset((a),(v),sizeof (a))
#define INF                          INT_MAX
#define mod 1000000007
#define __sync__		     std::ios::sync_with_stdio(false);
#define all(a)			     a.begin(),a.end()

#define TRACE

#ifdef TRACE
#define trace1(x)                cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;

#else

#define trace1(x)
#define trace2(x, y)
#define trace3(x, y, z)
#define trace4(a, b, c, d)
#define trace5(a, b, c, d, e)
#define trace6(a, b, c, d, e, f)

#endif

lld a[100010];

lld mpow(lld a, lld b)
{
	lld ret = 1;
	while(b)
	{
		if(b&1) ret = (ret*a)%mod;
		a = (a*a)%mod;
		b/=2;
	}
	return ret;
}

int main()
{
	int n,x,aa;
	cin>>n>>x;
	lld sum = 0;
	for(int i=0;i<n;i++)
	{
		cin>>aa;
		a[i] = aa;
		sum += aa;
	}
	for(int i=0;i<n;i++)
		a[i] = sum - a[i];
	lld ans = 0;
	sort(a,a+n);
	ans = a[0];
	lld cnt = count(a,a+n,a[0]);
	while(cnt && (cnt % x) == 0)
	{
		ans += 1;
		cnt/=x;
	}
	ans = min(sum,ans);
	cout<<mpow(x,ans)<<"\n";
	return 0;

}


#include<bits/stdc++.h>
using namespace std;

typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long int lld;

#define sz                           size()
#define pb                           push_back
#define mp                           make_pair
#define F                            first
#define S                            second
#define fill(a,v)                    memset((a),(v),sizeof (a))
#define INF                          INT_MAX
#define mod 1000000007
#define __sync__		     std::ios::sync_with_stdio(false);
#define all(a)			     a.begin(),a.end()

#define TRACE

#ifdef TRACE
#define trace1(x)                cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;

#else

#define trace1(x)
#define trace2(x, y)
#define trace3(x, y, z)
#define trace4(a, b, c, d)
#define trace5(a, b, c, d, e)
#define trace6(a, b, c, d, e, f)

#endif

#define int lld

lld a[100010];

lld mpow(lld a, lld b)
{
	lld ret = 1;
	while(b)
	{
		if(b&1) ret = (ret*a)%mod;
		a = (a*a)%mod;
		b/=2;
	}
	return ret;
}
#undef int
int main()
#define int lld
{
	int n,x,aa;
	cin>>n>>x;
	lld sum = 0;
	for(int i=0;i<n;i++)
	{
		cin>>aa;
		a[i] = aa;
		sum += aa;
	}
	for(int i=0;i<n;i++)
		a[i] = sum - a[i];
	lld ans = 0;
	sort(a,a+n);
	ans = a[0];
	lld cnt = count(a,a+n,a[0]);
	while(cnt && (cnt % x) == 0)
	{
		ans += 1;
		cnt/=x;
	}
	ans = min(sum,ans);
	cout<<mpow(x,ans)<<"\n";
	return 0;

}


#include<bits/stdc++.h>
using namespace std;

typedef vector<int> vi;
typedef pair<int,int> pii;
typedef long long int lld;

#define sz                           size()
#define pb                           push_back
#define mp                           make_pair
#define F                            first
#define S                            second
#define fill(a,v)                    memset((a),(v),sizeof (a))
#define INF                          INT_MAX
#define mod 1000000007
#define __sync__		     std::ios::sync_with_stdio(false);
#define all(a)			     a.begin(),a.end()

#define TRACE

#ifdef TRACE
#define trace1(x)                cerr << #x << ": " << x << endl;
#define trace2(x, y)             cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
#define trace3(x, y, z)          cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
#define trace4(a, b, c, d)       cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
#define trace5(a, b, c, d, e)    cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
#define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;

#else

#define trace1(x)
#define trace2(x, y)
#define trace3(x, y, z)
#define trace4(a, b, c, d)
#define trace5(a, b, c, d, e)
#define trace6(a, b, c, d, e, f)

#endif

#define int lld

lld a[100010];

lld mpow(lld A, lld B)
{
	lld ret = 1;
	while(B)
	{
		if(B&1) ret = (ret*A)%mod;
		A = (A*A)%mod;
		B/=2;
	}
	return ret;
}
#undef int
int main()
#define int lld
{
	int n,x,aa;
	cin>>n>>x;
	lld sum = 0;
	for(int i=0;i<n;i++)
	{
		cin>>aa;
		a[i] = aa;
		sum += aa;
	}
	for(int i=0;i<n;i++)
		a[i] = sum - a[i];
	lld ans = 0,ones = 0;
	sort(a,a+n);
	ones = count(a,a+n,a[0]);
	ans = a[0];
	int i = ones;
	while(1)
	{
		if(ones % x) break;
		ones/=x;
		ans++;
		while(i<n && a[i]-ans == 1) ones++,i++;
	}
	cout<<mpow(x,min(ans,sum))<<"\n";

	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <set>
#include <map>
using namespace std;

#define _int long long
#define INF 0x3f3f3f3f
//char s[10000];
//int ans[520000];
//_int B[110000];
//vector<_int>g;
_int A[110000+10];
_int B[110000+10];
_int F[110000+10];
_int L[110000+10];
//int a[1111],b[1111],c[1111];

//int dp[1005][1005];

const _int mod = 1e9+7;
int u[110000],v[110000],c[110000];
int n,x,k;
//_int gcd(_int x,_int y) {
//    if(x < y) swap(x,y);
//    if(y==0) return x;
//    else return gcd(y,x%y);
//}

_int power(_int a,_int b,_int Mod) {
    _int ans=1;
    x %= mod;
    while(b) {
        if(b&1) ans = (ans*a)%Mod;
        b/=2;
        a = (a*a)%Mod;
    }
    return ans%Mod;
}
vector<_int>g;
int main(){

    scanf("%d%d",&n,&x);
    _int sum=0;
    for(int i=1;i<=n;i++) {
        scanf("%lld",&A[i]);
        sum += A[i];
    }
    for(int i=1; i<=n; i++) {
        g.push_back(sum-A[i]);
    }
    sort(g.rbegin(),g.rend());
    int cnt=0;
    while(true) {
//        int len = g.size();
        _int u = g[g.size()-1];
        cnt=0;
        while(g.size()>0 && g[g.size()-1] == u) {
            cnt++;
            g.pop_back();
        }
        if(cnt % x != 0) {
            cout<<power(x,u,mod)<<endl;
            return 0;
        }
        else {
            cnt /= x;
            for(int j=1;j<=cnt; j++) {
                g.push_back(u+1);
            }
        }
    }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <set>
#include <map>
using namespace std;

#define _int long long
#define INF 0x3f3f3f3f
//char s[10000];
//int ans[520000];
//_int B[110000];
//vector<_int>g;
_int A[110000+10];
_int B[110000+10];
_int F[110000+10];
_int L[110000+10];
//int a[1111],b[1111],c[1111];

//int dp[1005][1005];

const int mod = 1e9+7;
int u[110000],v[110000],c[110000];
int n,x,k;
//_int gcd(_int x,_int y) {
//    if(x < y) swap(x,y);
//    if(y==0) return x;
//    else return gcd(y,x%y);
//}

_int power(_int a,_int b,_int Mod) {
    _int ans=1;
    x %= mod;
    while(b) {
        if(b&1) ans = (ans*a)%mod;
        b/=2;
        a = (a*a)%mod;
    }
    return ans;
}
vector<_int>g;
int main(){

    scanf("%d%d",&n,&x);
    _int sum=0;
    for(int i=1;i<=n;i++) {
        scanf("%lld",&A[i]);
        sum += A[i];
    }
    for(int i=1; i<=n; i++) {
        g.push_back(sum-A[i]);
    }
    sort(g.rbegin(),g.rend());
    int cnt=0;
    while(true) {
        int len = g.size();
        _int u = g[len-1];
        cnt=0;
        while(g.size() && g[g.size()-1] == u) {
            cnt++;
            g.pop_back();
        }
        if(cnt % x != 0) {
            cout<<power(x,u,mod)<<endl;
            return 0;
        }
        else {
            cnt /= x;
            for(int j=1;j<=cnt; j++) {
                g.push_back(u+1);
            }
        }
    }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <set>
#include <map>
using namespace std;

#define _int long long
#define INF 0x3f3f3f3f
//char s[10000];
//int ans[520000];
//_int B[110000];
//vector<_int>g;
_int A[110000+10];
_int B[110000+10];
_int F[110000+10];
_int L[110000+10];
//int a[1111],b[1111],c[1111];

//int dp[1005][1005];

const int mod = 1e9+7;
int u[110000],v[110000],c[110000];
int n,x,k;
//_int gcd(_int x,_int y) {
//    if(x < y) swap(x,y);
//    if(y==0) return x;
//    else return gcd(y,x%y);
//}

_int power(_int a,_int b,_int Mod) {
    _int ans=1;
    x %= mod;
    while(b) {
        if(b&1) ans = (ans*a)%Mod;
        b/=2;
        a = (a*a)%Mod;
    }
    return ans;
}
vector<_int>g;
int main(){

    scanf("%d%d",&n,&x);
    _int sum=0;
    for(int i=1;i<=n;i++) {
        scanf("%lld",&A[i]);
        sum += A[i];
    }
    for(int i=1; i<=n; i++) {
        g.push_back(sum-A[i]);
    }
    sort(g.rbegin(),g.rend());
    int cnt=0;
    while(true) {
        int len = g.size();
        _int u = g[len-1];
        cnt=0;
        while(g.size() && g[g.size()-1] == u) {
            cnt++;
            g.pop_back();
        }
        if(cnt % x != 0) {
            cout<<power(x,u,mod)<<endl;
            return 0;
        }
        else {
            cnt /= x;
            for(int j=1;j<=cnt; j++) {
                g.push_back(u+1);
            }
        }
    }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <set>
#include <map>
using namespace std;

#define _int long long
#define INF 0x3f3f3f3f
//char s[10000];
//int ans[520000];
//_int B[110000];
//vector<_int>g;
_int A[110000+10];
_int B[110000+10];
_int F[110000+10];
_int L[110000+10];
//int a[1111],b[1111],c[1111];

//int dp[1005][1005];

const _int mod = 1e9+7;
int u[110000],v[110000],c[110000];
int n,x,k;
//_int gcd(_int x,_int y) {
//    if(x < y) swap(x,y);
//    if(y==0) return x;
//    else return gcd(y,x%y);
//}

_int power(_int a,_int b,_int Mod) {
    _int ans=1;
    x %= mod;
    while(b) {
        if(b&1) ans = (ans*a)%Mod;
        b/=2;
        a = (a*a)%Mod;
    }
    return ans%Mod;
}
vector<_int>g;
int main(){

    scanf("%d%d",&n,&x);
    _int sum=0;
    for(int i=1;i<=n;i++) {
        scanf("%lld",&A[i]);
        sum += A[i];
    }
    for(int i=1; i<=n; i++) {
        g.push_back(sum-A[i]);
    }
    sort(g.rbegin(),g.rend());
    int cnt=0;
    while(true) {
//        int len = g.size();
        _int u = g[g.size()-1];
        cnt=0;
        while(g.size() && g[g.size()-1] == u) {
            cnt++;
            g.pop_back();
        }
        if(cnt % x != 0) {
            cout<<power(x,u,mod)<<endl;
            return 0;
        }
        else {
            cnt /= x;
            for(int j=1;j<=cnt; j++) {
                g.push_back(u+1);
            }
        }
    }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <set>
#include <map>
using namespace std;

#define _int long long
#define INF 0x3f3f3f3f
//char s[10000];
//int ans[520000];
//_int B[110000];
//vector<_int>g;
_int A[110000+10];
_int B[110000+10];
_int F[110000+10];
_int L[110000+10];
//int a[1111],b[1111],c[1111];

//int dp[1005][1005];

const _int mod = 1e9+7;
int u[110000],v[110000],c[110000];
int n,x,k;
//_int gcd(_int x,_int y) {
//    if(x < y) swap(x,y);
//    if(y==0) return x;
//    else return gcd(y,x%y);
//}

_int power(_int a,_int b,_int Mod) {
    _int ans=1;
    x %= mod;
    while(b) {
        if(b&1) ans = (ans*a)%Mod;
        b/=2;
        a = (a*a)%Mod;
    }
    return ans%Mod;
}
vector<_int>g;
int main(){

    scanf("%d%d",&n,&x);
    _int sum=0;
    for(int i=1;i<=n;i++) {
        scanf("%lld",&A[i]);
        sum += A[i];
    }
    for(int i=1; i<=n; i++) {
        g.push_back(sum-A[i]);
    }
    sort(g.rbegin(),g.rend());
    int cnt=0;
    while(true) {
//        int len = g.size();
        _int u = g[g.size()-1];
        cnt=0;
        while(g.size()>0 && g[g.size()-1] == u) {
            cnt++;
            g.pop_back();
        }
        if(cnt % x != 0) {
            u = min(u,sum);
            cout<<power(x,u,mod)<<endl;
            return 0;
        }
        else {
            cnt /= x;
            for(int j=1;j<=cnt; j++) {
                g.push_back(u+1);
            }
        }
    }

    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
const int mod = 1e9+7;
long long power(long long a, long long p){
	if(p==0)
		return 1;
	if(p==1)
		return a;
	return (power((a*a)%mod,p/2)*power(a,p%2))%mod;
}
int main(){
	ios_base::sync_with_stdio(false);
	int n,x;
	cin >> n >> x;
	vector<long long> arr(n);
	long long s = 0;
	for(int i = 0; i < n; ++i){
		cin >> arr[i];
		s+=arr[i];
	}
	priority_queue<int,vector<int>,greater<int> > pq;
	for(int i = 0; i < n; ++i){
		arr[i]=s-arr[i];
		pq.push(arr[i]);
	}
	while(!pq.empty()){
		int small = pq.top();
		int cnt = 1;
		pq.pop();
		bool added = false;
		while(!pq.empty()&&pq.top()==small){
			pq.pop();
			cnt++;
			if(cnt==x){
				pq.push(small+1);
				added=true;
				break;
			}
		}
		if(!added){
			cout << power(x,small) << '\n';
			return 0;
		}
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const long long mod = 1e9+7;
long long power(long long a, long long p){
	if(p==0)
		return 1;
	if(p==1)
		return a;
	return (power((a*a)%mod,p/2)*power(a,p%2))%mod;
}
int main(){
	ios_base::sync_with_stdio(false);
	int n,x;
	cin >> n >> x;
	long long sum = 0;
	vector<long long> arr(n);
	for(int i = 0; i < n; ++i){
		cin >> arr[i];
		sum+=arr[i];
	}
	for(int i = 0; i < n; ++i){
		arr[i]=sum-arr[i];
	}
	sort(arr.rbegin(),arr.rend());
	while(true){
		if(arr.size()<x){
			cout << power(x,(sum,arr.back())) << '\n';
			return 0;
		}
		long long small = arr.back();
		int cnt = 1;
		arr.pop_back();
		while(!arr.empty()&&arr.back()==small){
			cnt++;
			arr.pop_back();
		}
		if(cnt%x==0){
			for(int i = 0; i < cnt/x; ++i)
				arr.push_back(small+1);
		}
		else{
			cout << power(x,min(small,sum)) << '\n';
			return 0;
		}
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
const ll mod = 1e9+7;
long long power(long long a, long long p){
	if(p==0)
		return 1;
	if(p==1)
		return a;
	return (power((a*a)%mod,p/2)*power(a,p%2))%mod;
}
int main(){
	ios_base::sync_with_stdio(false);
	ll n,x;
	cin >> n >> x;
	vector<ll> arr(n);
	long long s = 0;
	for(ll i = 0; i < n; ++i){
		cin >> arr[i];
		s+=arr[i];
	}
	for(ll i = 0; i < n; ++i){
		arr[i]=s-arr[i];
	}
	sort(arr.rbegin(),arr.rend());
	while(true){
		if(arr.size()==1){
			cout << power(x,arr[0]) << '\n';
			return 0;
		}
		ll small = arr.back();
		ll cnt = 1;
		arr.pop_back();
		while(arr.back()==small){
			cnt++;
			arr.pop_back();
		}
		if(cnt%x==0){
			for(ll i = 0; i < cnt/x; ++i)
				arr.push_back(small+1);
		}
		else{
			cout << power(x,small) << '\n';
			return 0;
		}
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const long long mod = 1e9+7;
long long power(long long a, long long p){
	if(p==0)
		return 1;
	if(p==1)
		return a;
	return (power((a*a)%mod,p/2)*power(a,p%2))%mod;
}
int main(){
	ios_base::sync_with_stdio(false);
	int n,x;
	cin >> n >> x;
	long long sum = 0;
	vector<long long> arr(n);
	for(int i = 0; i < n; ++i){
		cin >> arr[i];
		sum+=arr[i];
	}
	for(int i = 0; i < n; ++i){
		arr[i]=sum-arr[i];
	}
	sort(arr.rbegin(),arr.rend());
	while(true){
		if(arr.size()<x){
			cout << power(x,arr.back()) << '\n';
			return 0;
		}
		long long small = arr.back();
		int cnt = 1;
		arr.pop_back();
		while(!arr.empty()&&arr.back()==small){
			cnt++;
			arr.pop_back();
		}
		if(cnt%x==0){
			for(int i = 0; i < cnt/x; ++i)
				arr.push_back(small+1);
		}
		else{
			cout << power(x,small) << '\n';
			return 0;
		}
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const long long mod = 1e9+7;
long long power(long long a, long long p){
	if(p==0)
		return 1;
	if(p==1)
		return a;
	return (power((a*a)%mod,p/2)*power(a,p%2))%mod;
}
int main(){
	ios_base::sync_with_stdio(false);
	int n,x;
	cin >> n >> x;
	long long sum = 0;
	vector<long long> arr(n);
	for(int i = 0; i < n; ++i){
		cin >> arr[i];
		sum+=arr[i];
	}
	for(int i = 0; i < n; ++i){
		arr[i]=sum-arr[i];
	}
	sort(arr.rbegin(),arr.rend());
	while(true){
		if(arr.size()<x){
			cout << power(x,min(sum,arr.back())) << '\n';
			return 0;
		}
		long long small = arr.back();
		int cnt = 1;
		arr.pop_back();
		while(!arr.empty()&&arr.back()==small){
			cnt++;
			arr.pop_back();
		}
		if(cnt%x==0){
			for(int i = 0; i < cnt/x; ++i)
				arr.push_back(small+1);
		}
		else{
			cout << power(x,min(small,sum)) << '\n';
			return 0;
		}
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const long long mod = 1e9+7;
long long power(long long a, long long p){
	if(p==0)
		return 1;
	if(p==1)
		return a;
	return (power((a*a)%mod,p/2)*power(a,p%2))%mod;
}
int main(){
	ios_base::sync_with_stdio(false);
	int n,x;
	cin >> n >> x;
	long long sum = 0;
	vector<long long> arr(n);
	for(int i = 0; i < n; ++i){
		cin >> arr[i];
		sum+=arr[i];
	}
	for(int i = 0; i < n; ++i){
		arr[i]=sum-arr[i];
	}
	sort(arr.rbegin(),arr.rend());
	while(true){
		if(arr.size()<x){
			cout << power(x,arr.back()) << '\n';
			return 0;
		}
		long long small = arr.back();
		int cnt = 1;
		arr.pop_back();
		while(arr.back()==small){
			cnt++;
			arr.pop_back();
		}
		if(cnt%x==0){
			for(int i = 0; i < cnt/x; ++i)
				arr.push_back(small+1);
		}
		else{
			cout << power(small,arr.back()) << '\n';
			return 0;
		}
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
typedef long long ll;
const ll mod = 1e9+7;
long long power(long long a, long long p){
	if(p==0)
		return 1;
	if(p==1)
		return a;
	return (power((a*a)%mod,p/2)*power(a,p%2))%mod;
}
int main(){
	ios_base::sync_with_stdio(false);
	ll n,x;
	cin >> n >> x;
	vector<long long> arr(n);
	long long s = 0;
	for(ll i = 0; i < n; ++i){
		cin >> arr[i];
		s+=arr[i];
	}
	for(ll i = 0; i < n; ++i)
		arr[i]=s-arr[i];
	sort(arr.rbegin(),arr.rend());
	while(true){
		if(arr.size()==1){
			cout << power(x,arr[0]) << '\n';
			return 0;
		}
		long long small = arr.back();
		ll cnt = 1;
		arr.pop_back();
		while(arr.back()==small){
			cnt++;
			arr.pop_back();
		}
		if(cnt%x==0){
			for(ll i = 0; i < cnt/x; ++i)
				arr.push_back(small+1);
		}
		else{
			cout << power(x,small) << '\n';
			return 0;
		}
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
const int mod = 1e9+7;
long long pow(long long a, long long p){
	if(p==0)
		return 1;
	if(p==1)
		return a;
	return (pow((a*a)%mod,p/2)*pow(a,p%2))%mod;
}
int main(){
	ios_base::sync_with_stdio(false);
	int n,x;
	cin >> n >> x;
	vector<long long> arr(n);
	long long s = 0;
	for(int i = 0; i < n; ++i){
		cin >> arr[i];
		s+=arr[i];
	}
	priority_queue<int,vector<int>,greater<int> > pq;
	for(int i = 0; i < n; ++i){
		arr[i]=s-arr[i];
		pq.push(arr[i]);
	}
	while(!pq.empty()){
		int small = pq.top();
		int cnt = 1;
		pq.pop();
		bool added = false;
		while(!pq.empty()&&pq.top()==small){
			pq.pop();
			cnt++;
			if(cnt==x){
				pq.push(small+1);
				added=true;
				break;
			}
		}
		if(!added){
			cout << pow(x,small) << '\n';
			return 0;
		}
	}
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
const int mod = 1e9+7;
long long power(long long a, long long p){
	if(p==0)
		return 1;
	if(p==1)
		return a;
	return (power((a*a)%mod,p/2)*power(a,p%2))%mod;
}
int main(){
	ios_base::sync_with_stdio(false);
	int n,x;
	cin >> n >> x;
	vector<int> arr(n);
	long long s = 0;
	for(int i = 0; i < n; ++i){
		cin >> arr[i];
		s+=arr[i];
	}
	for(int i = 0; i < n; ++i){
		arr[i]=s-arr[i];
	}
	sort(arr.rbegin(),arr.rend());
	while(true){
		if(arr.size()==1){
			cout << power(x,arr[0]) << '\n';
			return 0;
		}
		int small = arr.back();
		int cnt = 1;
		arr.pop_back();
		while(arr.back()==small){
			cnt++;
			arr.pop_back();
		}
		if(cnt%x==0){
			for(int i = 0; i < cnt/x; ++i)
				arr.push_back(small+1);
		}
		else{
			cout << power(x,small) << '\n';
			return 0;
		}
	}
}


#include<bits/stdc++.h>
#define ll long long

using namespace std;
const int N=1e5+5,mod=1e9+7;
ll a[N],n,x,cnt,sum,num[N];

inline ll fpow(ll a,ll b){
    ll res=1;
    while(b){
        if(b&1) res=res*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return res;
}

ll calc(ll x,ll y){
    ll res=0;
    while(x&&(x%y==0)){
        res++;
        x/=y;
    }
    return res;
}

int main(){
    scanf("%lld%lld",&n,&x);
    for(int i=0;i<n;i++){
        scanf("%lld",&a[i]);
        sum+=a[i];
    }
    for(int i=0;i<n;i++)
        num[i]=sum-a[i];
    sort(num,num+n);
    ll tot=1,now=num[0],tmp;
    int pos=0;
    while(233){
        //cout<<pos<<endl;
        while(pos+1<n&&num[pos+1]-cnt==now) tot++,pos++;
        //cout<<tot<<endl;
        cnt+=now;
        if((tot%x)||(now==0)){
            tmp=min(sum,cnt);
            ll ans=fpow(x,tmp);
            printf("%lld\n",ans);
            break;
        }
        now=calc(tot,x);
        if(pos+1<n) now=min(now,num[pos+1]-cnt);//从新定位当前最小的
        for(int i=1;i<=now;i++) tot/=x;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5;
const long long mod = 1e9+7;
int x,n,sum;
int a[maxn];
unsigned long long _pow[10000005];

unsigned long long gcd(long long x, long long y) {
	if (x%y == 0) return y;
	return gcd(y,x%y);
}

int main() {
	scanf("%d%d",&n,&x);
	for (int i = 1; i <= n; i++) {
		scanf("%d",&a[i]);
		sum += a[i];
	}
	_pow[0] = 1;
	for (int i = 1; i <= sum; i++) {
		_pow[i] = _pow[i-1]*(long long)x;
		_pow[i] %= mod;
	}
	long long son = 0;
	for (int i = 1; i <= n; i++) {
		son += _pow[sum-a[i]];
		son %= mod;
	}
	long long ans = gcd(son,_pow[sum])%mod;
	if (ans == 1 && _pow[sum] != 1) printf("%lld\n",son);
	else printf("%lld",ans);
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

const int N=1e5+5;
const int MOD=1e9+7;

int n,x;
int a[N];
int max_a;
map<long long,int>cnt;
long long sum=0;

long long power(long long a,long long b,long long mod){
    //a^b
    long long ret = 1;
    while(b){
        // ret, a < MOD = 1e9+7
        if(b%2) ret = (long long)ret*a%mod;
        a = (long long)a*a%mod;
        b /= 2;
    }
    return ret;
}

int main()
{
	cin>>n>>x;
	for(int i=1;i<=n;++i)
	{
		cin>>a[i];
		sum+=a[i];
		max_a=max(max_a,a[i]);
	}
	for(int i=1;i<=n;++i)
	{
		++cnt[sum-a[i]];
	}
	int c=0;
	for(map<long long,int>::iterator it=cnt.begin();it!=cnt.end();++it)
	{
		long long k=it->first;
		int c=it->second;
		if(c/x)cnt[k+1]+=c/x;
		c%=x;
		if(c){
			cout<<power(x,min(k,sum),MOD);
			return 0;
		}
	}
}


#include <bits/stdc++.h>
#define mod 1000000007
#define maxn 100005
using namespace std;
typedef long long ll;
ll x,n;
int a[maxn];
ll s;
map<ll,int> cnt;

ll quick_pow(ll a,ll b){
	ll ret=1,mul=a;
	while(b){
		if(b&1) ret=(mul*ret)%mod;
		b>>=1;
		mul=(mul*mul)%mod;
	}
	return ret;
}

int main(){
	scanf("%d%d",&n,&x);
	for(int i=0;i<n;i++){
		scanf("%d",&a[i]);
		s+=a[i];
	}
	for(int i=0;i<n;i++){
		cnt[s-a[i]]++;
	}
	for(map<ll,int>::iterator it=cnt.begin();it!=cnt.end();it++){
		ll k=it->first;
		int c=it->second;
		if(c/x) cnt[k+1]+=c/x;
		c%=x;
		if(c){
			printf("%lld",quick_pow(x,min(k,s)));
			return 0;
		}
	}
}


#include <bits/stdc++.h>
#define mod 1000000007
#define maxn 10005
using namespace std;
typedef long long ll;
ll x,n;
int a[maxn];
ll s;
map<ll,int> cnt;

ll quick_pow(ll a,ll b){
	ll ret=1,mul=a;
	while(b){
		if(b&1) ret=(mul*ret)%mod;
		b>>=1;
		mul=(mul*mul)%mod;
	}
	return ret;
}

int main(){
	scanf("%d%d",&n,&x);
	for(int i=0;i<n;i++){
		scanf("%d",&a[i]);
		s+=a[i];
	}
	for(int i=0;i<n;i++){
		cnt[s-a[i]]++;
	}
	for(map<ll,int>::iterator it=cnt.begin();it!=cnt.end();it++){
		ll k=it->first;
		int c=it->second;
		if(c/x) cnt[k+1]+=c/x;
		c%=x;
		if(c){
			printf("%lld",quick_pow(x,min(k,s)));
			return 0;
		}
	}
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=1e5 + 5;
const int mod=1000000007;
int n,x,sum,t,s,ans;
int a[maxn];
int f[maxn],fl[maxn];
map<int,int> mp;
int quick(int x,int y)
{
	int ret=1;
	int base=x;
	while(y)
	{
		if(y&1)
		{
			ret*=base;
			ret%=mod;
		}
		y>>=1;
		base*=base;
		base%=mod;
	}
	return ret%mod;
}
signed main()
{
	fl[0]=1;
	cin>>n>>x;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
		sum+=a[i];
	}
	for(int i=0;i<n;i++)
	{
		mp[sum-a[i]]++;
	}
	for(map<int,int>::iterator it=mp.begin();it!=mp.end();it++)
	{
		int k=it->first;
		int c=it->second;
		if(c/x)
		{
			f[k+1]+=c/x;
		}
		c%=x;
		if(c)
		{
			cout<<quick(x,min(k,sum));
			break;
		}
	}
}


#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=1e5 + 5;
const int mod=1000000007;
int n,x,sum,t,s,ans;
int a[maxn];
int f[maxn],fl[maxn];
int quick(int x,int y)
{
	int ret=1;
	int base=x;
	while(y)
	{
		if(y&1)
		{
			ret*=base;
			//ret%=mod;
		}
		y>>=1;
		base*=base;
		//base%=mod;
	}
	return ret;
}
signed main()
{
	fl[0]=1;
	cin>>n>>x;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
		sum+=a[i];
	}
	for(int i=0;i<n;i++)
	{
		f[i]=quick(x,a[i]);
	}
	t=quick(x,sum);
	for(int i=0;i<n;i++)
	{
		s+=t/f[i];
	}
	ans=__gcd(t,s);
	ans%=mod;
	cout<<ans;
}


#include <bits/stdc++.h>
using namespace std;

using ll = long long;
ll const M = 1e9 + 7;

unordered_map <ll, ll> mp;

ll pm(ll x, ll e) {
	ll r = 1;
	for (x%=M; e; e>>=1,x=x*x%M) if (e&1) r = r * x % M;
	return r;
}

int main() {
	ll n, x;
	cin >> n >> x;
	ll e = 0, mx = LLONG_MIN, mn = LLONG_MAX;
	for (ll i=0,a; i<n; ++i) {
		cin >> a;
		e += a;
		mx = max(mx, a);
		mn = min(mn, a);
		++mp[a];
	}
	e -= mx;
	ll m = mp[mx], t = 0;
	while (m % x == 0) m /= x, ++t;
	ll snd_large = -1;
	for (auto &p: mp) {
		if (p.first != mx && p.first > snd_large) snd_large = p.first;
	}
	if (mp.count(snd_large)) t = min(t, mx-snd_large);
	t = min(t, mx);
	e += t;
	cout << pm(x, e);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

using ll = long long;
ll const M = 1e9 + 7;

unordered_map <ll, ll> mp;

ll pm(ll x, ll e) {
	ll r = 1;
	for (x%=M; e; e>>=1,x=x*x%M) if (e&1) r = r * x % M;
	return r;
}

int main() {
	ll n, x;
	cin >> n >> x;
	ll e = 0, mx = LLONG_MIN, mn = LLONG_MAX;
	for (ll i=0,a; i<n; ++i) {
		cin >> a;
		e += a;
		mx = max(mx, a);
		mn = min(mn, a);
		++mp[a];
	}
	e -= mx;
	ll m = mp[mx], t = 0;
	while (m % x == 0) m /= x, ++t;
	ll snd_large = -1;
	for (auto &p: mp) {
		if (p.first != mx && p.first > snd_large) snd_large = p.first;
	}
	if (mp.count(snd_large)) t = min(t, mx-mp[snd_large]);
	t = min(t, mx);
	e += t;
	cout << pm(x, e);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

using ll = long long;
ll const M = 1e9 + 7;

unordered_map <ll, ll> mp;

ll pm(ll x, ll e) {
	ll r = 1;
	for (x%=M; e; e>>=1,x=x*x%M) if (e&1) r = r * x % M;
	return r;
}

int main() {
	ll n, x;
	cin >> n >> x;
	ll e = 0, mx = LLONG_MIN, mn = LLONG_MAX;
	for (ll i=0,a; i<n; ++i) {
		cin >> a;
		e += a;
		mx = max(mx, a);
		mn = min(mn, a);
		++mp[a];
	}
	e -= mx;
	ll m = mp[mx], t = 0;
	while (m % x == 0) m /= x, ++t;
	ll snd_large = -1;
	for (auto &p: mp) {
		if (p.first != mx && p.first > snd_large) snd_large = p.first;
	}
	if (mp.count(snd_large)) t = min(t, mp[snd_large]);
	t = min(t, mp[mx]);
	e += t;
	cout << pm(x, e);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

using ll = long long;
ll const M = 1e9 + 7;

ll pm(ll x, ll e) {
	ll r = 1;
	for (x%=M; e; e>>=1,x=x*x%M) if (e&1) r = r * x % M;
	return r;
}

int main() {
	ll n, x;
	cin >> n >> x;
	ll e = 0, mx = LLONG_MIN, mn = LLONG_MAX;
	for (ll i=0,a; i<n; ++i) {
		cin >> a;
		e += a;
		mx = max(mx, a);
		mn = min(mn, a);
	}
	if (mx == mn && !mx) {
		cout << 1;
	} else {
		e -= mx;
		if (mx == mn) ++e;
		cout << pm(x, e);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

using ll = long long;
ll const M = 1e9 + 7;

map <ll, ll> mp, ex;

ll pm(ll x, ll e) {
	ll r = 1;
	for (x%=M; e; e>>=1,x=x*x%M) if (e&1) r = r * x % M;
	return r;
}

int main() {
	ll n, x;
	cin >> n >> x;
	ll e = 0, mx = LLONG_MIN, mn = LLONG_MAX;
	for (ll i=0,a; i<n; ++i) {
		cin >> a;
		e += a;
		mx = max(mx, a);
		mn = min(mn, a);
		++mp[a];
	}
	e -= mx;
	for (auto &p: mp) ex[mx-p.first] = p.second;
	while (true) {
		bool ok = true;
		mp.clear();
		for (auto &p: ex) {
			ll t = p.first, c = p.second;
			if (c >= x) ok = false;
			vector <ll> o;
			do {
				o.push_back(c%x);
				c/=x;
			} while (c);
			for (int i=0; i<(int)o.size(); ++i) {
				mp[t+i] += o[i];
			}
		}
		ex = mp;
		if (ok) break;
	}
	for (auto &p: ex) {
		if (p.second) {
			e += p.first;
			break;
		}
	}
	cout << pm(x, e);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

using ll = long long;
ll const M = 1e9 + 7;

map <ll, ll> mp, ex;

ll pm(ll x, ll e) {
	ll r = 1;
	for (x%=M; e; e>>=1,x=x*x%M) if (e&1) r = r * x % M;
	return r;
}

int main() {
	ll n, x;
	cin >> n >> x;
	ll e = 0, mx = LLONG_MIN, mn = LLONG_MAX;
	for (ll i=0,a; i<n; ++i) {
		cin >> a;
		e += a;
		mx = max(mx, a);
		mn = min(mn, a);
		++mp[a];
	}
	e -= mx;
	for (auto &p: mp) ex[mx-p.first] = p.second;
	while (true) {
		bool ok = true;
		mp.clear();
		for (auto &p: ex) {
			ll t = p.first, c = p.second;
			if (c >= x) ok = false;
			vector <ll> o;
			do {
				o.push_back(c%x);
				c/=x;
			} while (c);
			for (int i=0; i<(int)o.size(); ++i) {
				mp[t+i] += o[i];
			}
		}
		ex = mp;
		if (ok) break;
	}
	for (auto &p: ex) {
		if (p.second) {
			e += min(mx, p.first);
			break;
		}
	}
	cout << pm(x, e);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

using ll = long long;
ll const M = 1e9 + 7;

ll pm(ll x, ll e) {
	ll r = 1;
	for (x%=M; e; e>>=1,x=x*x%M) if (e&1) r = r * x % M;
	return r;
}

int main() {
	ll n, x;
	cin >> n >> x;
	ll e = 0, mx = LLONG_MIN, mn = LLONG_MAX;
	for (ll i=0,a; i<n; ++i) {
		cin >> a;
		e += a;
		mx = max(mx, a);
		mn = min(mn, a);
	}
	e -= mx;
	if (mx == mn) ++e;
	cout << pm(x, e);
	return 0;
}


#include <cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<string>
#define min(a,b) (a<b?a:b)
using namespace std;
typedef long long ll;

const int maxn=1e5+10;
ll n,x;
ll val[maxn];
const int mod=1e9+7;

ll quick_pow(int x,ll n)
{
    ll ans=1;
    x=x%mod;
    while(n){
        if(n&1)
            ans=ans*x%mod;
        x=x*x%mod;
        n>>=1;
    }
    return ans;
}

int main()
{
    scanf("%I64d %I64d",&n,&x);
    ll sum=0;
    for(int i=0;i<n;i++){
        scanf("%I64d",&val[i]);
        sum += val[i];
    }
    for(int i=0;i<n;i++)
        val[i]=sum-val[i];
    sort(val,val+n);
    ll cnt=1;
    val[n]=-1;
    ll min_num;
    for(int i=1;i<=n;i++){
        if(val[i]==val[i-1])
            cnt++;
        else{
          if(cnt%x){
              min_num=val[i-1];
              break;
          }
          else{
             val[i-1]++;
             cnt/=x;
             i--;//返回上一个点，看看前面
          }
        }
    }
    ll ans_num=min(min_num,sum);
    printf("%I64d\n",quick_pow(x,ans_num));
    return 0;
}


#include <cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<string>
using namespace std;
typedef long long ll;

const int maxn=1e5+10;
int n,x;
ll val[maxn];
const int mod=1e9+7;

ll quick_pow(int x,ll n)
{
    ll ans=1;
    x=x%mod;
    while(n){
        if(n&1)
            ans=(ans*x)%mod;
        x=(x*x)%mod;
        n>>1;
    }
    return ans;
}

int main()
{
    scanf("%d %d",&n,&x);
    ll sum=0;
    for(int i=0;i<n;i++){
        scanf("%I64d",&val[i]);
        sum += val[i];
    }
    for(int i=0;i<n;i++)
        val[i]=sum-val[i];
    sort(val,val+n);
    int cnt=1;
    val[n]=-1;
    ll min_num;
    for(int i=1;i<=n;i++){
        if(val[i]==val[i-1])
            cnt++;
        else{
          if(cnt%x){
              min_num=val[i-1];
              break;
          }
          else{
             val[i-1]++;
             cnt/=x;
             i--;//返回上一个点，看看前面
          }
        }
    }
    ll ans_num=min(min_num,sum);
    printf("%I64d\n",quick_pow(x,ans_num));
    return 0;
}


#include <cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<string>
#define min(a,b) (a<b?a:b)
using namespace std;
typedef long long ll;

const int maxn=1e5+10;
ll n,x;
ll val[maxn];
const int mod=1e9+7;

ll quick_pow(ll x,ll n)
{
    ll ans=1;
    x=x%mod;
    while(n){
        if(n&1)
            ans=ans*x%mod;
        x=x*x%mod;
        n>>=1;
    }
    return ans;
}

int main()
{
    scanf("%I64d %I64d",&n,&x);
    ll sum=0;
    for(int i=0;i<n;i++){
        scanf("%I64d",&val[i]);
        sum += val[i];
    }
    for(int i=0;i<n;i++)
        val[i]=sum-val[i];
    sort(val,val+n);
    ll cnt=1;
    val[n]=-1;
    ll min_num;
    for(int i=1;i<=n;i++){
        if(val[i]==val[i-1])
            cnt++;
        else{
          if(cnt%x){
              min_num=val[i-1];
              break;
          }
          else{
             val[i-1]++;
             cnt/=x;
             i--;//返回上一个点，看看前面
          }
        }
    }
    ll ans_num=min(min_num,sum);
    printf("%I64d\n",quick_pow(x,ans_num));
    return 0;
}


#include <cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<string>
using namespace std;
typedef long long ll;

const int maxn=1e5+10;
ll n,x;
ll val[maxn];
const int mod=1e9+7;

ll quick_pow(int x,ll n)
{
    ll ans=1;
    x=x%mod;
    while(n){
        if(n&1)
            ans=(ans*x)%mod;
        x=(x*x)%mod;
        n>>=1;
    }
    return ans;
}

int main()
{
    scanf("%d %d",&n,&x);
    ll sum=0;
    for(int i=0;i<n;i++){
        scanf("%I64d",&val[i]);
        sum += val[i];
    }
    for(int i=0;i<n;i++)
        val[i]=sum-val[i];
    sort(val,val+n);
    ll cnt=1;
    val[n]=-1;
    ll min_num;
    for(int i=1;i<=n;i++){
        if(val[i]==val[i-1])
            cnt++;
        else{
          if(cnt%x){
              min_num=val[i-1];
              break;
          }
          else{
             val[i-1]++;
             cnt/=x;
             i--;//返回上一个点，看看前面
          }
        }
    }
    ll ans_num=min(min_num,sum);
    printf("%I64d\n",quick_pow(x,ans_num));
    return 0;
}


#include <cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<string>
using namespace std;
typedef long long ll;

const int maxn=1e5+10;
int n,x;
ll val[maxn];
const int mod=1e9+7;

ll quick_pow(int x,ll n)
{
    ll ans=1;
    x=x%mod;
    while(n){
        if(n&1)
            ans=(ans*x)%mod;
        x=(x*x)%mod;
        n>>=1;
    }
    return ans;
}

int main()
{
    scanf("%d %d",&n,&x);
    ll sum=0;
    for(int i=0;i<n;i++){
        scanf("%I64d",&val[i]);
        sum += val[i];
    }
    for(int i=0;i<n;i++)
        val[i]=sum-val[i];
    sort(val,val+n);
    int cnt=1;
    val[n]=-1;
    ll min_num;
    for(int i=1;i<=n;i++){
        if(val[i]==val[i-1])
            cnt++;
        else{
          if(cnt%x){
              min_num=val[i-1];
              break;
          }
          else{
             val[i-1]++;
             cnt/=x;
             i--;//返回上一个点，看看前面
          }
        }
    }
    ll ans_num=min(min_num,sum);
    printf("%I64d\n",quick_pow(x,ans_num));
    return 0;
}


#include <cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<string>
using namespace std;
typedef long long ll;

const int maxn=1e5+10;
int n,x;
ll val[maxn];
const int mod=1e9+7;

ll quick_pow(int x,ll n)
{
    ll ans=1;
    x=x%mod;
    while(n){
        if(n&1)
            ans=(ans*x)%mod;
        x=(x*x)%mod;
        n>>=1;
    }
    return ans;
}

int main()
{
    scanf("%d %d",&n,&x);
    ll sum=0;
    for(int i=0;i<n;i++){
        scanf("%I64d",&val[i]);
        sum += val[i];
    }
    for(int i=0;i<n;i++)
        val[i]=sum-val[i];
    sort(val,val+n);
    ll cnt=1;
    val[n]=-1;
    ll min_num;
    for(int i=1;i<=n;i++){
        if(val[i]==val[i-1])
            cnt++;
        else{
          if(cnt%x){
              min_num=val[i-1];
              break;
          }
          else{
             val[i-1]++;
             cnt/=x;
             i--;//返回上一个点，看看前面
          }
        }
    }
    ll ans_num=min(min_num,sum);
    printf("%I64d\n",quick_pow(x,ans_num));
    return 0;
}


#include <cstdio>
#include <algorithm>
#include <cctype>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
inline int read()
{
	int res  =0;
	char c;
	bool flag = false;
	c = getchar();
	while(!isdigit(c))
	{
		flag = (c == '-');
		c = getchar();
	}
	while(isdigit(c))
	{
		res = res * 10 + (c ^ 48);
		c = getchar();
	}
	return !flag ? res : -res;
}
inline int quickpow(int x , ll y)
{
	ll ret = 1 , base = x;
	while(y)
	{
		if(y & 1)	ret *= base , ret %= MOD;
		base *= base , base %= MOD;
		y >>= 1;
	}
	return ret;
}
const int Max_n = 1e5 + 5;
int n , x;
ll sum , st[Max_n] , a[Max_n] , tp;
inline bool cmp(ll a , ll b)
{
	return a > b;
}
int main()
{
	tp = n = read() , x = read();
	for(int i = 1 ; i <= n ; ++ i)
		a[i] = read() , sum += a[i];
	for(int i = 1 ; i <= n ; ++ i)
		st[i] = sum - a[i];
	sort(st + 1 , st + n + 1 , cmp);
	while(true)
	{
		int cnt = 0;
		ll sta = st[tp];
		if(sta >= sum)
		{
			printf("%d" , quickpow(x , sum));
			return 0;
		}
		while(sta == st[tp] && tp)
			++ cnt , -- tp;
		if(cnt % x)
		{
			printf("%d" , quickpow(x , sta));
			return 0;
		}
		cnt /= x;
		for(int i = 1 ; i <= cnt ; ++ i)
			st[ ++ tp] = sta + 1;
	}
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <cctype>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
inline int read()
{
	int res  =0;
	char c;
	bool flag = false;
	c = getchar();
	while(!isdigit(c))
	{
		flag = (c == '-');
		c = getchar();
	}
	while(isdigit(c))
	{
		res = res * 10 + (c ^ 48);
		c = getchar();
	}
	return !flag ? res : -res;
}
inline int quickpow(int x , int y)
{
	ll ret = 1 , base = x;
	while(y)
	{
		if(y & 1)	ret *= base , ret %= MOD;
		base *= base , base %= MOD;
		y >>= 1;
	}
	return ret;
}
const int Max_n = 1e5 + 5;
int n , x;
ll sum , st[Max_n] , a[Max_n] , tp;
inline bool cmp(int a , int b)
{
	return a > b;
}
int main()
{
	tp = n = read() , x = read();
	for(int i = 1 ; i <= n ; ++ i)
		a[i] = read() , sum += a[i];
	for(int i = 1 ; i <= n ; ++ i)
		st[i] = sum - a[i];
	sort(st + 1 , st + n + 1 , cmp);
	while(true)
	{
		int cnt = 0;
		ll sta = st[tp];
		if(sta >= sum)
		{
			printf("%d" , quickpow(x , sum));
			return 0;
		}
		while(sta == st[tp] && tp)
			++ cnt , -- tp;
		if(cnt % x)
		{
			printf("%d" , quickpow(x , sta));
			return 0;
		}
		cnt /= x;
		for(int i = 1 ; i <= cnt ; ++ i)
			st[ ++ tp] = sta + 1;
	}
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <cctype>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
inline int read()
{
	int res  =0;
	char c;
	bool flag = false;
	c = getchar();
	while(!isdigit(c))
	{
		flag = (c == '-');
		c = getchar();
	}
	while(isdigit(c))
	{
		res = res * 10 + (c ^ 48);
		c = getchar();
	}
	return !flag ? res : -res;
}
inline int quickpow(int x , int y)
{
	ll ret = 1 , base = x;
	while(y)
	{
		if(y & 1)	ret *= base , ret %= MOD;
		base *= base , base %= MOD;
		y >>= 1;
	}
	return ret;
}
const int Max_n = 1e5 + 5;
int n , x;
ll sum , st[Max_n] , a[Max_n] , tp;
inline bool cmp(ll a , ll b)
{
	return a > b;
}
int main()
{
	tp = n = read() , x = read();
	for(int i = 1 ; i <= n ; ++ i)
		a[i] = read() , sum += a[i];
	for(int i = 1 ; i <= n ; ++ i)
		st[i] = sum - a[i];
	sort(st + 1 , st + n + 1 , cmp);
	while(true)
	{
		int cnt = 0;
		ll sta = st[tp];
		if(sta >= sum)
		{
			printf("%d" , quickpow(x , sum));
			return 0;
		}
		while(sta == st[tp] && tp)
			++ cnt , -- tp;
		if(cnt % x)
		{
			printf("%d" , quickpow(x , sta));
			return 0;
		}
		cnt /= x;
		for(int i = 1 ; i <= cnt ; ++ i)
			st[ ++ tp] = sta + 1;
	}
	return 0;
}


#include <cstdio>
#include <algorithm>
#include <cctype>
using namespace std;
typedef long long ll;
const int MOD = 1e9 + 7;
inline int read()
{
	int res  =0;
	char c;
	bool flag = false;
	c = getchar();
	while(!isdigit(c))
	{
		flag = (c == '-');
		c = getchar();
	}
	while(isdigit(c))
	{
		res = res * 10 + (c ^ 48);
		c = getchar();
	}
	return !flag ? res : -res;
}
inline int quickpow(int x , int y)
{
	ll ret = 1 , base = x;
	while(y)
	{
		if(y & 1)	ret *= base , ret %= MOD;
		base *= base , base %= MOD;
		y >>= 1;
	}
	return ret;
}
const int Max_n = 1e5 + 5;
int n , x;
ll sum , st[Max_n] , a[Max_n] , tp;
inline bool cmp(int a , int b)
{
	return a > b;
}
int main()
{
	tp = n = read() , x = read();
	for(int i = 1 ; i <= n ; ++ i)
		a[i] = read() , sum += a[i];
	for(int i = 1 ; i <= n ; ++ i)
		st[i] = sum - a[i];
	sort(st + 1 , st + n + 1 , cmp);
	while(true)
	{
		int cnt = 0;
		ll sta = st[tp];
		while(sta == st[tp] && tp)
			++ cnt , -- tp;
		if(cnt % x)
		{
			printf("%d" , quickpow(x , min(sum , sta)));
			return 0;
		}
		cnt /= x;
		for(int i = 1 ; i <= cnt ; ++ i)
			st[ ++ tp] = sta + 1;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define md 1000000007
long long sum,x,n,a[100001],maxn;
long long ksm(long long a,long long b)
{
	long long res=1;
	while(b)
	{
		if(b&1) res=(res*a)%md;
		b>>=1;
		a=(a*a)%md;
	}
return res;
}
int main()
{
	scanf("%lld%lld",&n,&x);
	for(int i=1;i<=n;++i)
	  scanf("%lld",&a[i]),sum+=a[i];
	for(int i=1;i<=n;++i)
	  a[i]=sum-a[i];
	sort(a+1,a+n+1);
    long long tmp=1,ans;
    a[n+1]=-6371;
	for(int i=2;i<=n+1;++i)
	  {
	  	if(a[i]==a[i-1])
	  	  tmp++;
	  	  else{
	  	  	if(tmp%x==0)
	  	  	  {
	  	  	  	tmp/=x;
	  	  	  	a[i-1]+=1;
	  	  	  	i--;
				  }
			else {
				ans=a[i-1];
				goto here;
			}
			}
	  }
	here:
		tmp=ksm(x,ans);
	printf("%lld",tmp);
}


#include<bits/stdc++.h>
using namespace std;
#define md 1000000007
long long sum,x,n,a[100001],maxn;
long long ksm(long long a,long long b)
{
	long long res=1;
	while(b)
	{
		if(b&1) res=(res*a)%md;
		b>>=1;
		a=(a*a)%md;
	}
return res;
}
int main()
{
	scanf("%lld%lld",&n,&x);
	for(int i=1;i<=n;++i)
	  scanf("%lld",&a[i]),sum+=a[i],maxn=max(maxn,a[i]);
	long long tmp=ksm(x,sum-maxn);
	printf("%lld",tmp);
}


#include<bits/stdc++.h>
using namespace std;
#define md 1000000007
long long sum,x,n,a[100001],maxn;
long long ksm(long long a,long long b)
{
	long long res=1;
	while(b)
	{
		if(b&1) res=(res*a)%md;
		b>>=1;
		a=(a*a)%md;
	}
return res;
}
int main()
{
	scanf("%lld%lld",&n,&x);
	for(int i=1;i<=n;++i)
	  scanf("%lld",&a[i]),sum+=a[i];
	for(int i=1;i<=n;++i)
	  a[i]=sum-a[i];
	sort(a+1,a+n+1);
    long long tmp=0,ans;
    a[n+1]=-6371;
	for(int i=1;i<=n+1;++i)
	  {
	  	if(a[i]==a[i-1])
	  	  tmp++;
	  	  else{
	  	  	if(tmp%x==0)
	  	  	  {
	  	  	  	tmp/=x;
	  	  	  	a[i-1]+=1;
	  	  	  	i--;
				  }
			else {
				ans=a[i-1];
				goto here;
			}
			}
	  }
	here:
		tmp=ksm(x,ans);
	printf("%lld",tmp);
}


#include<bits/stdc++.h>
using namespace std;
#define md 1000000007
long long sum,x,n,a[100001],maxn;
long long ksm(long long a,long long b)
{
	long long res=1;
	while(b)
	{
		if(b&1) res=(res*a)%md;
		b>>=1;
		a=(a*a)%md;
	}
return res%md;
}
int main()
{
	scanf("%lld%lld",&n,&x);
	for(int i=1;i<=n;++i)
	  scanf("%lld",&a[i]),sum+=a[i];
	for(int i=1;i<=n;++i)
	  a[i]=sum-a[i];
	sort(a+1,a+n+1);
    long long tmp=1,ans;
    a[n+1]=-6371;
	for(int i=2;i<=n+1;++i)
	  {
	  	if(a[i]==a[i-1])
	  	  tmp++;
	  	  else{
	  	  	if(tmp%x==0)
	  	  	  {
	  	  	  	tmp/=x;
	  	  	  	a[i-1]+=1;
	  	  	  	i--;
				  }
			else {
				ans=a[i-1];
				goto here;
			}
			}
	  }
	here:
		ans=min(ans,sum);
		tmp=ksm(x,ans);
	printf("%lld",tmp);
}


#include<bits/stdc++.h>
using namespace std;
#define md 1000000007
long long sum,x,n,a[100001],maxn;
long long ksm(long long a,long long b)
{
	long long res=1;
	while(b)
	{
		if(b&1) res=(res*a)%md;
		b>>=1;
		a=(a*a)%md;
	}
return res;
}
int main()
{
	scanf("%lld%lld",&n,&x);
	for(int i=1;i<=n;++i)
	  scanf("%lld",&a[i]),sum+=a[i];
	for(int i=1;i<=n;++i)
	  a[i]=sum-a[i];
	sort(a+1,a+n+1);
    long long tmp=1,ans;
    a[n+1]=-6371;
	for(int i=1;i<=n+1;++i)
	  {
	  	if(a[i]==a[i-1])
	  	  tmp++;
	  	  else{
	  	  	if(tmp%x==0)
	  	  	  {
	  	  	  	tmp/=x;
	  	  	  	a[i-1]+=1;
	  	  	  	i--;
				  }
			else {
				ans=a[i-1];
				goto here;
			}
			}
	  }
	here:
		tmp=ksm(x,ans);
	printf("%lld",tmp);
}


#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define INPUT freopen("359.inp","r",stdin)
#define OUTPUT freopen("359.out","w",stdout)
#define FOR(i,l,r) for(auto i=l;i<=r;i++)
#define REP(i,l,r) for(auto i=l;i<r;i++)
#define FORD(i,l,r) for(auto i=l;i>=r;i--)
#define REPD(i,l,r) for(auto i=l;i>r;i--)
#define ENDL printf("\n")
#define debug 1

typedef long long ll;
typedef pair<int,int> ii;

const int inf=1e9;
const int MOD=1e9+7;
const int N=1e5+10;

int n,x,a[N];
ll suf[N],pre[N],s[N];
priority_queue <ll> q;
ll power(ll x,ll y){
    ll ans=1;
    while (y){
        if (y&1) ans=ans*x%MOD;
        x=x*x%MOD;
        y>>=1;
    }
    return ans;
}
ll solve(){
    FOR(i,1,n) pre[i]=pre[i-1]+a[i];
    FORD(i,n,1) suf[i]=suf[i+1]+a[i];
    FOR(i,1,n) q.push(-pre[i-1]-suf[i+1]);
    while (1){
        ll cur=q.top();
        int len=0;
        while (!q.empty()&&q.top()==cur) len++,q.pop();
        int v=0;
        while (len%x==0) v--,len/=x;
        q.push(cur+v);
        if (len>1||v==0) break;
    }
//    cout<<s[top]<<'\n';
    return power(x,min(-q.top(),pre[n]));
}
int main(){
    scanf("%d%d",&n,&x);
    FOR(i,1,n) scanf("%d",a+i);
    cout<<solve();
}


#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define INPUT freopen("359.inp","r",stdin)
#define OUTPUT freopen("359.out","w",stdout)
#define FOR(i,l,r) for(auto i=l;i<=r;i++)
#define REP(i,l,r) for(auto i=l;i<r;i++)
#define FORD(i,l,r) for(auto i=l;i>=r;i--)
#define REPD(i,l,r) for(auto i=l;i>r;i--)
#define ENDL printf("\n")
#define debug 1

typedef long long ll;
typedef pair<int,int> ii;

const int inf=1e9;
const int MOD=1e9+7;
const int N=2e5+10;

int n,x,a[N];
ll suf[N],pre[N],s[N];
ll power(ll x,ll y){
    ll ans=1;
    while (y){
        if (y&1) ans=ans*x%MOD;
        x=x*x%MOD;
        y>>=1;
    }
    return ans;
}
int solve(){
    FOR(i,1,n) pre[i]=pre[i-1]+a[i];
    FORD(i,n,1) suf[i]=suf[i+1]+a[i];
    FOR(i,1,n) s[i]=pre[i-1]+suf[i+1];
    int top=n;
//    FOR(i,1,top) printf("%lld ",s[i]);
    while (top>1){
        int len=1;
        while (len<=top&&s[top-len]==s[top]) len++;
        top-=len-1;
        int v=0;
        while (len%x==0) v++,len/=x;
        s[top]+=v;
        if (len>1||!v) break;
//        printf("%d\n",top);
//        FOR(i,1,top) printf("%lld ",s[i]);ENDL;
    }
    return power(x,s[top]);
}
int main(){
    scanf("%d%d",&n,&x);
    FOR(i,1,n) scanf("%d",a+i);
    cout<<solve();
}


#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define INPUT freopen("359.inp","r",stdin)
#define OUTPUT freopen("359.out","w",stdout)
#define FOR(i,l,r) for(auto i=l;i<=r;i++)
#define REP(i,l,r) for(auto i=l;i<r;i++)
#define FORD(i,l,r) for(auto i=l;i>=r;i--)
#define REPD(i,l,r) for(auto i=l;i>r;i--)
#define ENDL printf("\n")
#define debug 1

typedef long long ll;
typedef pair<int,int> ii;

const int inf=1e9;
const int MOD=1e9+7;
const int N=2e5+10;

int n,x,a[N];
ll suf[N],pre[N],s[N];
ll power(ll x,ll y){
    ll ans=1;
    while (y){
        if (y&1) ans=ans*x%MOD;
        x=x*x%MOD;
        y>>=1;
    }
    return ans;
}
int solve(){
    FOR(i,1,n) pre[i]=pre[i-1]+a[i];
    FORD(i,n,1) suf[i]=suf[i+1]+a[i];
    FOR(i,1,n) s[i]=pre[i-1]+suf[i+1];
    int top=n;
//    FOR(i,1,top) printf("%lld ",s[i]);
    while (1){
        int len=1;
        while (len<top&&s[top-len]==s[top]) len++;
        top-=len-1;
        int v=0;
        while (len%x==0) v++,len/=x;
        s[top]+=v;
        if (len>1||!v) break;
//        printf("%d\n",top);
//        FOR(i,1,top) printf("%lld ",s[i]);ENDL;
    }
//    cout<<s[top]<<'\n';
    return power(x,s[top]);
}
int main(){
    scanf("%d%d",&n,&x);
    FOR(i,1,n) scanf("%d",a+i);
    cout<<solve();
}


#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define INPUT freopen("359.inp","r",stdin)
#define OUTPUT freopen("359.out","w",stdout)
#define FOR(i,l,r) for(auto i=l;i<=r;i++)
#define REP(i,l,r) for(auto i=l;i<r;i++)
#define FORD(i,l,r) for(auto i=l;i>=r;i--)
#define REPD(i,l,r) for(auto i=l;i>r;i--)
#define ENDL printf("\n")
#define debug 1

typedef long long ll;
typedef pair<int,int> ii;

const int inf=1e9;
const int MOD=1e9+7;
const int N=2e5+10;

int n,x,a[N];
ll suf[N],pre[N],s[N];
ll power(ll x,ll y){
    ll ans=1;
    while (y){
        if (y&1) ans=ans*x%MOD;
        x=x*x%MOD;
        y>>=1;
    }
    return ans;
}
int solve(){
    FOR(i,1,n) pre[i]=pre[i-1]+a[i];
    FORD(i,n,1) suf[i]=suf[i+1]+a[i];
    FOR(i,1,n) s[i]=pre[i-1]+suf[i+1];
    int top=n;
    while (1){
        int len=1;
        while (top>1&&s[top-1]==s[top]) len++,top--;
        int v=0;
        while (len%x==0) v++,len/=x;
        s[top]+=v;
        if (len>1||v==0) break;
//        printf("%d\n",top);
//        FOR(i,1,top) printf("%lld ",s[i]);ENDL;
    }
//    cout<<s[top]<<'\n';
    return power(x,s[top]);
}
int main(){
    scanf("%d%d",&n,&x);
    FOR(i,1,n) scanf("%d",a+i);
    cout<<solve();
}


#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define INPUT freopen("359.inp","r",stdin)
#define OUTPUT freopen("359.out","w",stdout)
#define FOR(i,l,r) for(auto i=l;i<=r;i++)
#define REP(i,l,r) for(auto i=l;i<r;i++)
#define FORD(i,l,r) for(auto i=l;i>=r;i--)
#define REPD(i,l,r) for(auto i=l;i>r;i--)
#define ENDL printf("\n")
#define debug 1

typedef long long ll;
typedef pair<int,int> ii;

const int inf=1e9;
const int MOD=1e9+7;
const int N=2e5+10;

int n,x,a[N];
ll suf[N],pre[N],s[N];
ll power(ll x,ll y){
    ll ans=1;
    while (y){
        if (y&1) ans=ans*x%MOD;
        x=x*x%MOD;
        y>>=1;
    }
    return ans;
}
int solve(){
    FOR(i,1,n) pre[i]=pre[i-1]+a[i];
    FORD(i,n,1) suf[i]=suf[i+1]+a[i];
    FOR(i,1,n) s[n-i+1]=pre[i-1]+suf[i+1];
    int top=n;
//    FOR(i,1,top) printf("%lld ",s[i]);
    while (top>1){
        int len=1;
        while (len<=top&&s[top-len]==s[top]) len++;
        top-=len-1;
        int v=0;
        while (len%x==0) v++,len/=x;
        s[top]+=v;
        if (len>1) break;
//        printf("%d\n",top);
//        FOR(i,1,top) printf("%lld ",s[i]);ENDL;
    }
    return power(x,s[top]);
}
int main(){
    scanf("%d%d",&n,&x);
    FOR(i,1,n) scanf("%d",a+i);
    cout<<solve();
}


#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define INPUT freopen("359.inp","r",stdin)
#define OUTPUT freopen("359.out","w",stdout)
#define FOR(i,l,r) for(auto i=l;i<=r;i++)
#define REP(i,l,r) for(auto i=l;i<r;i++)
#define FORD(i,l,r) for(auto i=l;i>=r;i--)
#define REPD(i,l,r) for(auto i=l;i>r;i--)
#define ENDL printf("\n")
#define debug 1

typedef long long ll;
typedef pair<int,int> ii;

const int inf=1e9;
const int MOD=1e9+7;
const int N=1e5+10;

int n,x,a[N];
ll suf[N],pre[N],s[N];
ll power(ll x,ll y){
    ll ans=1;
    while (y){
        if (y&1) ans=ans*x%MOD;
        x=x*x%MOD;
        y>>=1;
    }
    return ans;
}
int solve(){
    FOR(i,1,n) pre[i]=pre[i-1]+a[i];
    FORD(i,n,1) suf[i]=suf[i+1]+a[i];
    FOR(i,1,n) s[i]=pre[i-1]+suf[i+1];
    int top=n;
    while (1){
        int len=1;
        while (top>1&&s[top-1]==s[top]) len++,top--;
        int v=0;
        while (len%x==0) v++,len/=x;
        s[top]+=v;
        if (len>1||v==0) break;
    }
    return power(x,min(s[top],pre[n]));
}
int main(){
    scanf("%d%d",&n,&x);
    FOR(i,1,n) scanf("%d",a+i);
    cout<<solve();
}


#include <bits/stdc++.h>

using namespace std;

#define X first
#define Y second
#define INPUT freopen("359.inp","r",stdin)
#define OUTPUT freopen("359.out","w",stdout)
#define FOR(i,l,r) for(auto i=l;i<=r;i++)
#define REP(i,l,r) for(auto i=l;i<r;i++)
#define FORD(i,l,r) for(auto i=l;i>=r;i--)
#define REPD(i,l,r) for(auto i=l;i>r;i--)
#define ENDL printf("\n")
#define debug 1

typedef long long ll;
typedef pair<int,int> ii;

const int inf=1e9;
const int MOD=1e9+7;
const int N=1e5+10;

int n,x,a[N];
ll suf[N],pre[N],s[N];
priority_queue <ll> q;
ll power(ll x,ll y){
    ll ans=1;
    while (y){
        if (y&1) ans=ans*x%MOD;
        x=x*x%MOD;
        y>>=1;
    }
    return ans;
}
ll solve(){
    FOR(i,1,n) pre[i]=pre[i-1]+a[i];
    FORD(i,n,1) suf[i]=suf[i+1]+a[i];
    FOR(i,1,n) q.push(-pre[i-1]-suf[i+1]);
    while (1){
        ll cur=q.top();
        int len=0;
        while (!q.empty()&&q.top()==cur&&len<x) len++,q.pop();
        q.push(cur-(len==x));
        if (len<x) break;
    }
//    cout<<s[top]<<'\n';
    return power(x,min(-q.top(),pre[n]));
}
int main(){
    scanf("%d%d",&n,&x);
    FOR(i,1,n) scanf("%d",a+i);
    cout<<solve();
}


#include <bits/stdc++.h>
#define MOD 1000000007
using namespace std;
typedef long long ll;

ll exp(ll x, ll p)
{
	ll b = 1;
	while (p) {
		if (p & 1) {
			b = (x * b) % MOD;
		}
		x = (x * x) % MOD;
		p >>= 1;
	}
	return b % MOD;
}

int main()
{
	int n, x;
	cin >> n >> x;
	int p = -1;
	vector<pair<int, int>> v;
	pair<int, int> cur = { -1, 0 };
	ll sum = 0;
	for (int i = 0; i < n; ++i) {
		int d;
		scanf("%d", &d);
		if (d == p) {
			++cur.second;
		} else {
			if (i != 0) v.push_back(cur);
			cur = { d, 1 };
			p = d;
		}
		sum += d;
	}
	v.push_back(cur);
	ll pow = sum - v.back().first;
	ll add = v.back().first;
	for (int i = v.size() - 1; i >= 0; --i) {
		if ((add - v[i].first) != 0) {
			pow += (add - v[i].first);
			add -= (add - v[i].first);
			v[i].first = 0;
		} else if (v[i].second % x == 0) {
			++pow;
			--add;
			v[i] = { v[i].first, v[i].second / x };
		} else {
			break;
		}
	}
	cout << exp(x, pow) << endl;
	return 0;
}


#include <bits/stdc++.h>
#define MOD 1000000007
using namespace std;
typedef long long ll;

ll exp(ll x, ll p)
{
	ll b = 1;
	while (p) {
		if (p & 1) {
			b = (x * b) % MOD;
		}
		x = (x * x) % MOD;
		p >>= 1;
	}
	return b % MOD;
}

int main()
{
	int n, x;
	cin >> n >> x;
	ll sum = 0;
	vector<ll> v;
	for (int i = 0; i < n; ++i) {
		int d;
		scanf("%d", &d);
		v.push_back(d);
		sum += d;
	}
	for (int i = 0; i < v.size(); ++i) {
		v[i] = sum - v[i];
	}
	while (1) {
		int i = v.size() - 1;
		while (i > 0 && v[i - 1] == v.back()) {
			--i;
		}
		int cnt = v.size() - i;
		if (cnt % x) {
			break;
		}
		int old = v.back();
		v.resize(i);
		for (int i = 0; i < cnt / x; ++i) {
			v.push_back(old + 1);
		}
	}
	cout << exp(x, min(sum, v.back())) << endl;
	return 0;
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Codeforces
{
    class Program
    {

        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            string[] s = Console.ReadLine().Split();

            long police = 0;
            int crime = 0;

            for (int i = 0; i < n; i++)
            {
                int foo = int.Parse(s[i]);
                if (foo == -1)
                    if (police > 0)
                        police--;
                    else
                        crime++;
                else
                    police += foo;
            }

            Console.WriteLine(crime);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;

namespace Active
{
	public class Solver
	{
		public void Solve()
		{
			int n = Console.ReadLine().ToInt();
			var data = Console.ReadLine().SplitToStringList();
			int ans = 0;
			int sum = 0;
			for (int i = 0; i < n; i++)
			{
				sum += data[i].ToInt();
				if (sum < 0)
				{
					ans = Math.Max(ans, Math.Abs(sum));
				}
			}
			Console.WriteLine(ans);
		}
	}

	class Program
	{
		static void Main(string[] args)
		{
			new Solver().Solve();
		}
	}

	public static class Helper
	{
		public static T Convert<T>(this string s)
		{
			try
			{
				return (T)System.Convert.ChangeType(s, typeof (T));
			}
			catch (Exception ex)
			{
				throw new FormatException(String.Format("Не получилось конвертнуть в {0}", typeof(T)));
			}
		}

		public static List<string> SplitToStringList(this string s)
		{
			return s
				.Split(new[] {' '}, StringSplitOptions.RemoveEmptyEntries)
				.ToList();
		}

		public static int ToInt(this string s)
		{
			return int.Parse(s);
		}

		public static double ToDouble(this string s)
		{
			return double.Parse(s);
		}
	}
}


 using System;
 class Program
    {
        static void Main(string[] args)
        {
            string[] input = Console.In.ReadToEnd().Split(new char[] { ' ', '\t', '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
            int cntr = 0;
            for(int i = 1, curr, cops=0; i<input.Length; ++i)
            {
                curr = int.Parse(input[i]);
                if (curr == -1)
                {
                    if (cops == 0) cntr++;
                    else cops--;
                }
                else cops+=curr;
            }
            Console.WriteLine(cntr);
        }
    }


using System;

namespace Question1
{
    partial class Question1
    {
        static void Main(string[] args)
        {
            int n, m, officer = 0, count = 0;

            n = NextInt();

            for (int i = 0; i < n; i++)
            {
                m = NextInt();

                if (m == -1)
                {
                    if (officer > 0)
                        officer--;
                    else
                        count++;
                }
                else
                {
                    officer += m;
                }
            }

            Console.WriteLine(count);
        }
    }

    partial class Question1
    {
        static string[] tokens = new string[0];
        static int cur_token = 0;

        static void ReadArray(char sep)
        {
            cur_token = 0;
            tokens = Console.ReadLine().Split(sep);
        }

        static int ReadInt()
        {
            return int.Parse(Console.ReadLine());
        }

        static long ReadLong()
        {
            return long.Parse(Console.ReadLine());
        }

        static int NextInt()
        {
            if (cur_token == tokens.Length)
                ReadArray(' ');
            return int.Parse(tokens[cur_token++]);
        }

        static long NextLong()
        {
            if (cur_token == tokens.Length)
                ReadArray(' ');
            return long.Parse(tokens[cur_token++]);
        }

        static double NextDouble()
        {
            if (cur_token == tokens.Length)
                ReadArray(' ');
            return double.Parse(tokens[cur_token++]);
        }

        static decimal NextDecimal()
        {
            if (cur_token == tokens.Length)
                ReadArray(' ');
            return decimal.Parse(tokens[cur_token++]);
        }

        static string NextToken()
        {
            if (cur_token == tokens.Length)
                ReadArray(' ');
            return tokens[cur_token++];
        }

        static string ReadLine()
        {
            return Console.ReadLine();
        }
    }
}


using System;
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            var n = Int32.Parse(Console.ReadLine());
            var str = Console.ReadLine().Split(' ');
            var pol = 0;
            var res = 0;
            for(var i = 0; i < str.Length; ++i)
            {
                var e = Int32.Parse(str[i]);
                if(e == -1)
                {
                    if (pol == 0)
                        res += 1;
                    else
                        --pol;
                }

                else
                {
                    pol += e;
                }
            }
            Console.WriteLine(res);

        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;

// Codeforces problem 427A "Полицейские-рекруты"
// http://codeforces.com/problemset/problem/427/A
namespace _427_A
{
	class Program
	{
		static void Main(string[] args)
		{
			#region Read data
			int n = Convert.ToInt32(Console.ReadLine());
			List<sbyte> events = new List<sbyte>(Console.ReadLine().Split().Select(sbyte.Parse));
			#endregion
			#region Process data
			int availableCops = 0;
			int unhandledCrimes = 0;
			foreach (var e in events)
			{
				if (e > 0)
				{
					availableCops += e;
				}
				if (e == -1)
				{
					if (availableCops < 1)
					{
						unhandledCrimes++;
					}
					else
					{
						availableCops--;
					}
				}
			}
			#endregion
			#region Write results
			Console.WriteLine(unhandledCrimes);
			Console.ReadLine();
			#endregion
		}
	}
}


using System;
using System.Collections.Generic;
using System.Linq;

// Codeforces problem 427A "Полицейские-рекруты"
// http://codeforces.com/problemset/problem/427/A
namespace _427_A
{
	class Program
	{
		static void Main(string[] args)
		{
			#region Read data
			int n = Convert.ToInt32(Console.ReadLine());
			List<sbyte> events = new List<sbyte>(Console.ReadLine().Split().Select(sbyte.Parse));
			#endregion
			#region Process data
			int availableCops = 0;
			int unhandledCrimes = 0;
			foreach (var e in events)
			{
				if (e > 0)
					availableCops += e;
				else if (availableCops < 1) unhandledCrimes++;
				else availableCops--;
			}
			#endregion
			#region Write results
			Console.WriteLine(unhandledCrimes);
			Console.ReadLine();
			#endregion
		}
	}
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

     class Program
     {
        static void Main(string[] args)
        {
            int a = int.Parse(Console.ReadLine());
            int[] mas = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
            int e = 0,k=0;
            for (int i = 0; i < mas.Length; i++)
            {
              e+=mas[i];
              if (e < 0) { k++; e = 0; }
            }
            Console.Write(k);
            Console.ReadLine ();
        }
     }


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    class Class1
    {
        public static void Main(string[] args)
        {
            var firstLine = Console.ReadLine();

            var numberOfEvents = Int32.Parse(firstLine);

            var input = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();

            int numberOfPolice = 0;
            int numberOfUntreated = 0;
            for(int i=0; i<numberOfEvents; i++)
            {
                if (input[i] > 0) numberOfPolice = numberOfPolice + input[i];
                else
                {
                    if (numberOfPolice > 0)
                    {
                        numberOfPolice--;
                    }
                    else
                    {
                        numberOfUntreated++;
                    }
                }
            }

            Console.WriteLine(numberOfUntreated);
        }

    }
}


using System;
using System.Collections.Generic;
using System.Linq;
class Solver
{
    int[] dx = { 0, -1, 0, 1 };
    int[] dy = { -1, 0, 1, 0 };
    const int mod = (int)1e9 + 7;
    void Solve()
    {
        var n = sc.Integer();
        var a = sc.IntArray(n);
        var sum = 0;
        long ret=0;
        foreach (var x in a)
        {
            if (x == -1)
            {
                if (sum == 0)
                    ret++;
                else sum--;
            }
            else sum += x;
        }
        Printer.PrintLine(ret);
    }

    static void Main()
    {
#if DEBUG
        var ostream = new System.IO.FileStream("debug.txt", System.IO.FileMode.Create, System.IO.FileAccess.Write);
        var iStream = new System.IO.FileStream("input.txt", System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);
        Console.SetIn(new System.IO.StreamReader(iStream));
        System.Diagnostics.Debug.AutoFlush = true;
        System.Diagnostics.Debug.Listeners.Add(new System.Diagnostics.TextWriterTraceListener(new System.IO.StreamWriter(ostream, System.Text.Encoding.UTF8)));
        try
        {
#endif
            var solver = new Solver();
            solver.Solve();
#if DEBUG
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            Console.WriteLine(ex.StackTrace);
        }
        Console.ReadKey(true);
#endif
    }
    _Scanner sc = new _Scanner();

}
static public class Printer
{
    static readonly private System.IO.TextWriter writer;
    static readonly private System.Globalization.CultureInfo info;
    static Printer()
    {
        writer = Console.Out;
        info = System.Globalization.CultureInfo.InvariantCulture;
    }

    static public void Print(int num) { writer.Write(num.ToString(info)); }
    static public void Print(int num,string format) { writer.Write(num.ToString(format,info)); }
    static public void Print(long num) { writer.Write(num.ToString(info)); }
    static public void Print(long num,string format) { writer.Write(num.ToString(format,info)); }
    static public void Print(double num) { writer.Write(num.ToString(info)); }
    static public void Print(double num, string format) { writer.Write(num.ToString(format, info)); }
    static public void Print(string str) { writer.Write(str); }
    static public void Print(string format, params object[] arg) { writer.Write(format, arg); }
    static public void Print(IEnumerable<char> sources) { writer.Write(sources.AsString()); }
    static public void Print<T>(IEnumerable<T> sources, string separator = " ")
    {
        var res = new System.Text.StringBuilder();
        foreach (var x in sources)
        {
            res.AppendFormat(info, "{0}", x);
            if (!string.IsNullOrEmpty(separator))
                res.Append(separator);
        }
        writer.Write(res.ToString(0, res.Length - separator.Length));
    }
    static public void PrintLine(int num) { writer.WriteLine(num.ToString(info)); }
    static public void PrintLine(int num, string format) { writer.WriteLine(num.ToString(format, info)); }
    static public void PrintLine(long num) { writer.WriteLine(num.ToString(info)); }
    static public void PrintLine(long num, string format) { writer.WriteLine(num.ToString(format, info)); }
    static public void PrintLine(double num) { writer.WriteLine(num.ToString(info)); }
    static public void PrintLine(double num, string format) { writer.WriteLine(num.ToString(format, info)); }
    static public void PrintLine(string str) { writer.WriteLine(str); }
    static public void PrintLine(string format, params object[] arg) { writer.WriteLine(format, arg); }
    static public void PrintLine(IEnumerable<char> sources) { writer.WriteLine(sources.AsString()); }
    static public void PrintLine<T>(IEnumerable<T> sources, string separator = " ")
    {
        var res = new System.Text.StringBuilder();
        foreach (var x in sources)
        {
            res.AppendFormat(info, "{0}", x);
            if (!string.IsNullOrEmpty(separator))
                res.Append(separator);
        }
        writer.Write(res.ToString(0, res.Length - separator.Length));
    }
}
public class _Scanner
{
    readonly private System.Globalization.CultureInfo info;
    readonly System.IO.TextReader reader;
    string[] buffer = new string[0];
    int position;

    public char[] Separator { get; set; }
    public _Scanner(System.IO.TextReader reader = null, string separator = null, System.Globalization.CultureInfo info = null)
    {

        this.reader = reader ?? Console.In;
        if (string.IsNullOrEmpty(separator))
            separator = " ";
        this.Separator = separator.ToCharArray();
        this.info = info ?? System.Globalization.CultureInfo.InvariantCulture;
    }
    public string Scan()
    {
        if (this.position < this.buffer.Length)
            return this.buffer[this.position++];
        this.buffer = this.reader.ReadLine().Split(this.Separator, StringSplitOptions.RemoveEmptyEntries);
        this.position = 0;
        return this.buffer[this.position++];
    }

    public string[] ScanToEndLine()
    {
        if (this.position >= this.buffer.Length)
            return this.reader.ReadLine().Split(this.Separator, StringSplitOptions.RemoveEmptyEntries);
        var size = this.buffer.Length - this.position;
        var ar = new string[size];
        Array.Copy(this.buffer, position, ar, 0, size);
        return ar;

    }

    public string ScanLine()
    {
        if (this.position >= this.buffer.Length)
            return this.reader.ReadLine();
        else
        {
            var sb = new System.Text.StringBuilder();
            for (; this.position < buffer.Length; this.position++)
            {
                sb.Append(this.buffer[this.position]);
                sb.Append(' ');
            }
            return sb.ToString();
        }
    }
    public string[] ScanArray(int length)
    {
        var ar = new string[length];
        for (int i = 0; i < length; i++)
        {
            ar[i] = this.Scan();
        }
        return ar;
    }

    public int Integer()
    {
        return int.Parse(this.Scan(), info);
    }
    public long Long()
    {
        return long.Parse(this.Scan(), info);
    }
    public double Double()
    {
        return double.Parse(this.Scan(), info);
    }

    public int[] IntArray(int length)
    {
        var a = new int[length];
        for (int i = 0; i < length; i++)
            a[i] = this.Integer();
        return a;
    }
    public long[] LongArray(int length)
    {
        var a = new long[length];
        for (int i = 0; i < length; i++)
            a[i] = this.Long();
        return a;
    }
    public double[] DoubleArray(int length)
    {
        var a = new double[length];
        for (int i = 0; i < length; i++)
            a[i] = this.Double();
        return a;
    }

}
static public partial class EnumerableEx
{
    static public string AsString(this IEnumerable<char> source)
    {
        return new string(source.ToArray());
    }
    static public IEnumerable<T> Enumerate<T>(this int count, Func<int, T> selector)
    {
        return Enumerable.Range(0, count).Select(x => selector(x));
    }
}
//*/


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] a = new int[100005];

            int n = Convert.ToInt32(Console.ReadLine());

            a = Console.ReadLine().Split().Select(int.Parse).ToArray();

            int p = 0, ans = 0;
            for(int i  = 0; i < n; i ++)
            {
                if (a[i] == -1)
                {
                    if(p >= 1)
                    {
                        p--;
                    }
                    else
                    {
                        ans++;
                    }
                }
                else p += a[i];
            }

            Console.WriteLine("{0}\n", ans);
        }
    }
}