#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <queue>
#include <math.h>
#include <stack>
#include <map>

#define ll long long
#define fs first
#define sc second
#define mp make_pair

const ll INF = 2000000000000000;
const double PI = 3.14159265358979323846;
const ll MOD = 1000000000 + 7;

using namespace std;

ll n, x;
ll st[100005], t[100005], sum;
pair <ll, ll> mint;

ll bin_pow(ll a, ll v) {
    if (v == 0)
        return 1;
    if (v % 2 == 1) {
        return (a * bin_pow(a, v-1)) % MOD;
    }
    else {
        ll t = bin_pow(a, v/2) % MOD;
        return t * t % MOD;
    }
}

int main() {
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
#endif
    mint.first = INF;
    cin >> n >> x;
    bool p = 0;
    for (int i(0); i<n; i++) {
        cin >> t[i];
        sum += t[i];
        if (t[i] == 0)
            p = 1;
    }

    if (p) {
        cout << 1;
        return 0;
    }

    for (int i(0); i<n; i++) {
        st[i] = sum-t[i];
    }
    int k = 1;
    for (int i(0); i<n; i++) {
        if (i != n-1 && st[i] == st[i+1]) {
            k++;
        }
        else {
            ll z = st[i] + k / x;
            if (z != st[i+1]) {
                if (mint.first > z) {
                    mint.first = z;
                    mint.second = k % x;
                    if (mint.second == 0)
                        mint.second = 1;
                }
                k = 1;
            }
            else {
                k = k / x + 1;
            }
        }
    }

    if (mint.first >= sum) {
        mint.first = sum;
        mint.second = 1;
    }

    cout << (bin_pow(x, mint.first) * mint.second) % MOD;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <queue>
#include <math.h>
#include <stack>
#include <map>

#define ll long long
#define fs first
#define sc second
#define mp make_pair

const ll INF = 2000000000000000;
const double PI = 3.14159265358979323846;
const ll MOD = 1000000000 + 7;

using namespace std;

ll n, x;
ll st[100005], t[100005], sum;
ll mint;

ll bin_pow(ll a, ll v) {
    if (v == 0)
        return 1;
    if (v % 2 == 1) {
        return (a * bin_pow(a, v-1)) % MOD;
    }
    else {
        ll t = bin_pow(a, v/2) % MOD;
        return t * t % MOD;
    }
}

int main() {
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
#endif
    mint = INF;
    cin >> n >> x;
    bool p = 0;
    for (int i(0); i<n; i++) {
        cin >> t[i];
        sum += t[i];
        if (t[i] == 0)
            p = 1;
    }

    if (p) {
        cout << 1;
        return 0;
    }

    for (int i(0); i<n; i++) {
        st[i] = sum-t[i];
    }

    reverse (st, st + n);

    int k = 1;
    for (int i(0); i<n; i++) {
        if (i != n-1 && st[i] == st[i+1]) {
            k++;
        }
        else {
            ll z = st[i] + k / x;
            if (z != st[i+1]) {
                mint = z;
                if (k % x != 0) {
                    mint = st[i];
                    break;
                }
                k = 1;
            }
            else {
                k = k / x + 1;
            }
        }
    }

    if (mint >= sum) {
        mint = sum;
    }

    cout << bin_pow(x, mint);
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <queue>
#include <math.h>
#include <stack>
#include <map>

#define ll long long
#define fs first
#define sc second
#define mp make_pair

const ll INF = 2000000000000000;
const double PI = 3.14159265358979323846;
const ll MOD = 1000000000 + 7;

using namespace std;

ll n, x;
ll st[100005], t[100005], sum;
ll mint;
map<ll, ll> q;

ll bin_pow(ll a, ll v) {
    if (v == 0)
        return 1;
    if (v % 2 == 1) {
        return (a * bin_pow(a, v-1)) % MOD;
    }
    else {
        ll t = bin_pow(a, v/2) % MOD;
        return t * t % MOD;
    }
}

int main() {
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
#endif
    mint = INF;
    cin >> n >> x;
    bool p = 0;
    for (int i(0); i<n; i++) {
        cin >> t[i];
        sum += t[i];
        if (t[i] == 0)
            p = 1;
    }

    if (p) {
        cout << 1;
        return 0;
    }

    for (int i(0); i<n; i++) {
        st[i] = sum-t[i];
    }

    sort(st, st+n);

    int k = 1;
    for (int i(0); i<n; i++) {
        if (i != n-1 && st[i] == st[i+1]) {
            k++;
        }
        else {
            if (k % x != 0) {
                mint = st[i];
                break;
            }
            else {
                k /= x;
                while (k > 0) {
                    st[i]++;
                    i--;
                    k--;
                }
                k = 1;
            }
        }
    }

    if (mint >= sum) {
        mint = sum;
    }

    cout << bin_pow(x, mint);
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <queue>
#include <math.h>
#include <stack>
#include <map>

#define ll long long
#define fs first
#define sc second
#define mp make_pair

const ll INF = 2000000000000000;
const double PI = 3.14159265358979323846;
const ll MOD = 1000000000 + 7;

using namespace std;

ll n, x;
ll st[100005], t[100005], sum;
pair <ll, ll> mint;

ll bin_pow(ll a, ll v) {
    if (v == 0)
        return 1;
    if (v % 2 == 1) {
        return (a * bin_pow(a, v-1)) % MOD;
    }
    else {
        ll t = bin_pow(a, v/2) % MOD;
        return t * t % MOD;
    }
}

int main() {
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
#endif
    mint.first = INF;
    cin >> n >> x;
    for (int i(0); i<n; i++) {
        cin >> t[i];
        sum += t[i];
    }
    for (int i(0); i<n; i++) {
        st[i] = sum-t[i];
    }
    int k = 1;
    for (int i(0); i<n; i++) {
        if (i != n-1 && st[i] == st[i+1]) {
            k++;
        }
        else {
            ll z = st[i] + k / x;
            if (mint.first > z) {
                mint.first = z;
                mint.second = k % x;
                if (mint.second == 0)
                    mint.second = 1;
            }
            k = 1;
        }
    }
    if (mint.first >= sum) {
        mint.first = sum;
        mint.second = 1;
    }

    cout << (bin_pow(x, mint.first) * mint.second) % MOD;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <queue>
#include <math.h>
#include <stack>
#include <map>

#define ll long long
#define fs first
#define sc second
#define mp make_pair

const ll INF = 2000000000000000;
const double PI = 3.14159265358979323846;
const ll MOD = 1000000000 + 7;

using namespace std;

ll n, x;
ll st[100005], t[100005], sum;
ll mint;

ll bin_pow(ll a, ll v) {
    if (v == 0)
        return 1;
    if (v % 2 == 1) {
        return (a * bin_pow(a, v-1)) % MOD;
    }
    else {
        ll t = bin_pow(a, v/2) % MOD;
        return t * t % MOD;
    }
}

int main() {
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
#endif
    mint = INF;
    cin >> n >> x;
    bool p = 0;
    for (int i(0); i<n; i++) {
        cin >> t[i];
        sum += t[i];
        if (t[i] == 0)
            p = 1;
    }

    if (p) {
        cout << 1;
        return 0;
    }

    for (int i(0); i<n; i++) {
        st[i] = sum-t[i];
    }

    reverse (st, st + n);

    int k = 1;
    for (int i(0); i<n; i++) {
        if (i != n-1 && st[i] == st[i+1]) {
            k++;
        }
        else {
            ll z = st[i] + k / x;
            if (z != st[i+1] && k % x != 0) {
                mint = st[i];
                break;
            }
            else {
                k = k / x + 1;
            }
        }
    }

    if (mint >= sum) {
        mint = sum;
    }

    cout << bin_pow(x, mint);
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <queue>
#include <math.h>
#include <stack>
#include <map>

#define ll long long
#define fs first
#define sc second
#define mp make_pair

const ll INF = 2000000000000000;
const double PI = 3.14159265358979323846;
const ll MOD = 1000000000 + 7;

using namespace std;

int n, x;
long long st[100005], t[100005], sum;
pair <ll, ll> mint;

ll bin_pow(ll a, ll v) {
    if (v == 0)
        return 1;
    if (v % 2 == 1) {
        return (a * bin_pow(a, v-1)) % MOD;
    }
    else {
        ll t = bin_pow(a, v/2);
        return t * t % MOD;
    }
}

int main() {
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
#endif
    mint.first = INF;
    cin >> n >> x;
    for (int i(0); i<n; i++) {
        cin >> t[i];
        sum += t[i];
    }
    for (int i(0); i<n; i++) {
        st[i] = sum-t[i];
    }
    int k = 0;
    for (int i(0); i<n; i++) {
        if (i != n-1 && st[i] == st[i+1]) {
            k++;
        }
        else {
            ll z = st[i] + k / x;
            if (mint.first > z) {
                mint.first = z;
                mint.second = k % x + 1;
            }
            k = 0;
        }
    }
    if (mint.first >= sum) {
        mint.first = sum;
        mint.second = 1;
    }

    cout << (bin_pow(x, mint.first) * mint.second) % MOD;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <queue>
#include <math.h>
#include <stack>
#include <map>

#define ll long long
#define fs first
#define sc second
#define mp make_pair

const ll INF = 2000000000000000;
const double PI = 3.14159265358979323846;
const ll MOD = 1000000000 + 7;

using namespace std;

ll n, x;
ll st[100005], t[100005], sum;
pair <ll, ll> mint;

ll bin_pow(ll a, ll v) {
    if (v == 0)
        return 1;
    if (v % 2 == 1) {
        return (a * bin_pow(a, v-1)) % MOD;
    }
    else {
        ll t = bin_pow(a, v/2) % MOD;
        return t * t % MOD;
    }
}

int main() {
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
#endif
    mint.first = INF;
    cin >> n >> x;
    for (int i(0); i<n; i++) {
        cin >> t[i];
        sum += t[i];
    }
    for (int i(0); i<n; i++) {
        st[i] = sum-t[i];
    }
    int k = 1;
    for (int i(0); i<n; i++) {
        if (i != n-1 && st[i] == st[i+1]) {
            k++;
        }
        else {
            ll z = st[i] + k / x;
            if (mint.first > z) {
                mint.first = z;
                mint.second = k % x;
                if (mint.second == 0)
                    mint.second = 1;
            }
            k = 1;
            if (z == st[i+1])
                k+=mint.second;
        }
    }
    if (mint.first >= sum) {
        mint.first = sum;
        mint.second = 1;
    }

    cout << (bin_pow(x, mint.first) * mint.second) % MOD;
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <queue>
#include <math.h>

#define ll long long
#define fs first
#define sc second
#define mp make_pair

const ll INF = 2000000000000000;
const double PI = 3.14159265358979323846;
const ll MOD = 1000000000 + 7;

using namespace std;

ll n, x;
ll st[100005], t[100005], sum;
ll mint;

ll bin_pow(ll a, ll v) {
    if (v == 0)
        return 1;
    if (v % 2 == 1) {
        return (a * bin_pow(a, v-1)) % MOD;
    }
    else {
        ll t = bin_pow(a, v/2) % MOD;
        return t * t % MOD;
    }
}

int main() {
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
#endif
    mint = INF;
    cin >> n >> x;
    bool p = 0;
    for (int i(0); i<n; i++) {
        cin >> t[i];
        sum += t[i];
        if (t[i] == 0)
            p = 1;
    }

    for (int i(0); i<n; i++) {
        st[i] = sum-t[i];
    }

    sort(st, st+n);

    int k = 1;
    for (int i(0); i<n; i++) {
        if (i != n-1 && st[i] == st[i+1]) {
            k++;
        }
        else {
            if (k % x != 0) {
                mint = st[i];
                break;
            }
            else {
                k /= x;
                while (k > 0) {
                    st[i]++;
                    i--;
                    k--;
                }
                k = 1;
            }
        }
    }

    if (mint >= sum) {
        mint = sum;
    }

    cout << bin_pow(x, mint);
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <queue>
#include <math.h>
#include <stack>
#include <map>

#define ll long long
#define fs first
#define sc second
#define mp make_pair

const ll INF = 2000000000000000;
const double PI = 3.14159265358979323846;
const ll MOD = 1000000000 + 7;

using namespace std;

ll n, x;
ll st[100005], t[100005], sum;
ll mint;
map<ll, ll> q;

ll bin_pow(ll a, ll v) {
    if (v == 0)
        return 1;
    if (v % 2 == 1) {
        return (a * bin_pow(a, v-1)) % MOD;
    }
    else {
        ll t = bin_pow(a, v/2) % MOD;
        return t * t % MOD;
    }
}

int main() {
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
#endif
    mint = INF;
    cin >> n >> x;
    bool p = 0;
    for (int i(0); i<n; i++) {
        cin >> t[i];
        sum += t[i];
        if (t[i] == 0)
            p = 1;
    }

    for (int i(0); i<n; i++) {
        st[i] = sum-t[i];
    }

    sort(st, st+n);

    int k = 1;
    for (int i(0); i<n; i++) {
        if (i != n-1 && st[i] == st[i+1]) {
            k++;
        }
        else {
            if (k % x != 0) {
                mint = st[i];
                break;
            }
            else {
                k /= x;
                while (k > 0) {
                    st[i]++;
                    i--;
                    k--;
                }
                k = 1;
            }
        }
    }

    if (mint >= sum) {
        mint = sum;
    }

    cout << bin_pow(x, mint);
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <queue>
#include <math.h>
#include <stack>

#define ll long long
#define fs first
#define sc second
#define mp make_pair

const ll INF = 2000000000000000;
const double PI = 3.14159265358979323846;
const ll MOD = 1000000000 + 7;

using namespace std;

ll n, x;
ll st[100005], t[100005], sum;
ll mint;
map<ll, ll> q;

ll bin_pow(ll a, ll v) {
    if (v == 0)
        return 1;
    if (v % 2 == 1) {
        return (a * bin_pow(a, v-1)) % MOD;
    }
    else {
        ll t = bin_pow(a, v/2) % MOD;
        return t * t % MOD;
    }
}

int main() {
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
#endif
    mint = INF;
    cin >> n >> x;
    bool p = 0;
    for (int i(0); i<n; i++) {
        cin >> t[i];
        sum += t[i];
        if (t[i] == 0)
            p = 1;
    }

    if (p) {
        cout << 1;
        return 0;
    }

    for (int i(0); i<n; i++) {
        st[i] = sum-t[i];
    }

    sort(st, st+n);
    reverse (st, st + n);

    int k = 1;
    for (int i(0); i<n; i++) {
        if (i != n-1 && st[i] == st[i+1]) {
            k++;
        }
        else {
            if (k % x != 0) {
                mint = st[i];
                break;
            }
            else {
                k /= x;
                while (k > 0) {
                    st[i]++;
                    i--;
                    k--;
                }
                k = 1;
            }
        }
    }

    if (mint >= sum) {
        mint = sum;
    }

    cout << bin_pow(x, mint);
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <queue>
#include <math.h>
#include <stack>
#include <map>

#define ll long long
#define fs first
#define sc second
#define mp make_pair

const ll INF = 2000000000000000;
const double PI = 3.14159265358979323846;
const ll MOD = 1000000000 + 7;

using namespace std;

ll n, x;
ll st[100005], t[100005], sum;
ll mint;
map<ll, ll> q;

ll bin_pow(ll a, ll v) {
    if (v == 0)
        return 1;
    if (v % 2 == 1) {
        return (a * bin_pow(a, v-1)) % MOD;
    }
    else {
        ll t = bin_pow(a, v/2) % MOD;
        return t * t % MOD;
    }
}

int main() {
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
#endif
    mint = INF;
    cin >> n >> x;
    bool p = 0;
    for (int i(0); i<n; i++) {
        cin >> t[i];
        sum += t[i];
        if (t[i] == 0)
            p = 1;
    }

    if (p) {
        cout << 1;
        return 0;
    }

    for (int i(0); i<n; i++) {
        st[i] = sum-t[i];
    }

    sort(st, st+n);
    reverse (st, st + n);

    int k = 1;
    for (int i(0); i<n; i++) {
        if (i != n-1 && st[i] == st[i+1]) {
            k++;
        }
        else {
            if (k % x != 0) {
                mint = st[i];
                break;
            }
            else {
                k /= x;
                while (k > 0) {
                    st[i]++;
                    i--;
                    k--;
                }
                k = 1;
            }
        }
    }

    if (mint >= sum) {
        mint = sum;
    }

    cout << bin_pow(x, mint);
}


#include <iostream>
#include <stdio.h>
#include <vector>
#include <string>
#include <algorithm>
#include <set>
#include <queue>
#include <math.h>
#include <stack>
#include <map>

#define ll long long
#define fs first
#define sc second
#define mp make_pair

const ll INF = 2000000000000000;
const double PI = 3.14159265358979323846;
const ll MOD = 1000000000 + 7;

using namespace std;

ll n, x;
ll st[100005], t[100005], sum;
pair <ll, ll> mint;

ll bin_pow(ll a, ll v) {
    if (v == 0)
        return 1;
    if (v % 2 == 1) {
        return (a * bin_pow(a, v-1)) % MOD;
    }
    else {
        ll t = bin_pow(a, v/2) % MOD;
        return t * t % MOD;
    }
}

int main() {
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
#endif
    mint.first = INF;
    cin >> n >> x;
    bool p = 0;
    for (int i(0); i<n; i++) {
        cin >> t[i];
        sum += t[i];
        if (t[i] == 0)
            p = 1;
    }

    if (p) {
        cout << 1;
        return 0;
    }

    for (int i(0); i<n; i++) {
        st[i] = sum-t[i];
    }

    reverse (st, st + n);

    int k = 1;
    for (int i(0); i<n; i++) {
        if (i != n-1 && st[i] == st[i+1]) {
            k++;
        }
        else {
            ll z = st[i] + k / x;
            if (z != st[i+1]) {
                if (mint.first > z) {
                    mint.first = z;
                    mint.second = k % x;
                    if (mint.second == 0)
                        mint.second = 1;
                }
                k = 1;
            }
            else {
                k = k / x + 1;
            }
        }
    }

    if (mint.first >= sum) {
        mint.first = sum;
        mint.second = 1;
    }

    cout << (bin_pow(x, mint.first) * mint.second) % MOD;
}


/* ********************************************
*    *     *      *    * * * *     * * * *    *
*   * *   * *   ***    *     *    *       *   *
*   *  * *  *  ****    * * *      *       *   *
*   *   *   *     *    * *        *       *   *
*   *       *     *    *  *       *       *   *
*   *       *     *    *   *      *       *   *
*   *       *     *    *    *      * * * *    *
********************************************* */
#include <fstream>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cassert>
/////////////////////
#define MP make_pair
#define FOR(v,p,k) for(int v=p;v<=k;++v)
#define FORD(v,p,k) for(int v=p;v>=k;--v)
#define REP(i,n) for(int i=0;i<(n);++i)
#define VAR(v,i) __typeof(i) v=(i)
#define FOREACH(i,c) for(__typeof(c.begin()) i=(c.begin());i!=(c).end();++i)
#define PB push_back
#define ST first
#define ND second
#define ABS(x)(x < 0)?(-x):(x)
#define SIZE(x) (int)x.size()
#define ALL(c) c.begin(),c.end()
#define MAX(a,b)(a > b)?(a):(b)
#define MIN(a,b)(a > b)?(b):(a)
#define ZERO(x) memset(x,0,sizeof(x))
#define PI acos(-1)
#define eps 1e-13
#define MOD 1000000007

using namespace std;
typedef long long LL;
const int NMAX = 100024;

int n, x;

LL pow(LL x, LL v) {
    if(v == 0) return 1 % MOD;
    if(v % 2 == 1) return (x * pow(x, v - 1)) % MOD;
    LL r = pow(x, v / 2);
    return (r * r) % MOD;
}

int main(){
    cin >> n >> x;
    vector <LL> a(n), b;
    LL sum = 0;
    for(int i = 0; i < n; i++)
      cin >> a[i],sum += a[i];
    for(int i = 0; i < n; i++)
        b.PB(sum - a[i]);
    sort(b.rbegin(), b.rend());
    while (true) {
        LL v = b[b.size() - 1];
        int cnt = 0;
        while (b.size() > 0 && b[b.size() - 1] == v) {
            cnt ++;
            b.pop_back();
        }
        if (cnt % x != 0) {
            v = min(v, sum);
            cout << pow(x, v) << endl;
            return 0;
        } else {
            cnt /= x;
            for(int i = 0; i < cnt; i++)
                b.push_back(v + 1);
        }
    }
    return 0;
}


#include <iostream>

using namespace std;

#include <cmath>
#include <string>
#include <ctime>
#include <stdlib.h>
#include <algorithm>

int prime;
int const base = 1000000007;

long long mm (long long a, long long b)
{if (a<b) return a; return b;}

int f (long long deg)
{
	if (deg ==0) return 1;
	long long fun;
	fun = f (deg /2);
	fun = fun % base;
	fun = fun*fun;
	fun = fun % base;
	if (deg&1) fun *= prime;
	fun = fun % base;
	return fun;
}

long long a [111111];

int main()
{
	//freopen ("input.txt", "r", stdin); freopen ("output.txt", "w", stdout);
	int n;
	long long sum=0;
	cin >> n >> prime;
	for (int i=0; i<n; ++i)
	{
		cin >> a[i];
		sum+=a[i];
	}
	long long sumsum = sum;
	long long min=sum;
	for (int i=0; i<n; ++i){
		if ((sum-a[i])<min) min=sum-a[i];
		a[i] = sum-a[i];
	}
	for (int i=0; i<n; ++i) a[i]-=min;
	sort(a,a+n);
	int deg=0;
	sum =0;
	a[n]=1000000010;
	for (int i =0; i<=n; i++)
	{
		if (a[i]==deg) {sum++; continue;}
		while (a[i]>deg)
		{
			if (sum%prime !=0) {cout << f(mm(min, sumsum))<< endl; return 0;};
			if (sum%prime ==0) {sum = sum /prime; deg ++; min ++;}
		}
		//deg=a[i];
		sum++;
	}

	return 0;
}


#include <iostream>
using namespace std;
#include <cmath>
#include <string>
#include <ctime>
#include <stdlib.h>
#include <algorithm>

int prime;
int const base = 1000000007;
int f (long long deg)
{
	if (deg ==0) return 1;
	long long fun;
	fun = f (deg /2);
	fun = fun % base;
	fun = fun*fun;
	fun = fun % base;
	if (deg&1) fun *= prime;
	fun = fun % base;
	return fun;
}

long long a [111111];

int main()
{
	//freopen ("input.txt", "r", stdin); freopen ("output.txt", "w", stdout);
	int n;
	long long sum=0;
	cin >> n >> prime;
	for (int i=0; i<n; ++i)
	{
		cin >> a[i];
		sum+=a[i];
	}

	long long min=sum;
	for (int i=0; i<n; ++i){
		if ((sum-a[i])<min) min=sum-a[i];
		a[i] = sum-a[i];
	}
	for (int i=0; i<n; ++i) a[i]-=min;
	sort(a,a+n);
	int deg=0;
	sum =0;
	a[n]=2000000010;
	for (int i =0; i<=n; i++)
	{
		if (a[i]==deg) {sum++; continue;}
		while (a[i]>deg)
		{
			if (sum%prime !=0) {cout << f(min)<< endl; return 0;};
			if (sum%prime ==0) {sum = sum /prime; deg++; min++;}
		}
		//deg=a[i];
		sum++;
	}

	return 0;
}


#include <iostream>
using namespace std;
#include <cmath>
#include <string>
#include <ctime>
#include <stdlib.h>
#include <algorithm>

int prime;
int const base = 1000000007;
int f (int deg)
{
	if (deg ==0) return 1;
	long long fun;
	fun = f (deg /2);
	fun = fun % base;
	fun = fun*fun;
	fun = fun % base;
	if (deg&1) fun *= prime;
	fun = fun % base;
	return fun;
}

int a [111111];

int main()
{
	//freopen ("input.txt", "r", stdin); freopen ("output.txt", "w", stdout);
	int n,sum=0;
	cin >> n >> prime;
	for (int i=0; i<n; ++i)
	{
		cin >> a[i];
		sum+=a[i];
	}

	int min=sum;
	for (int i=0; i<n; ++i){
		if ((sum-a[i])<min) min=sum-a[i];
		a[i] = sum-a[i];
	}
	for (int i=0; i<n; ++i) a[i]-=min;
	sort(a,a+n);
	int deg=0;
	sum =0;
	a[n]=1000000010;
	for (int i =0; i<=n; i++)
	{
		if (a[i]==deg) {sum++; continue;}
		while (a[i]>deg)
		{
			if (sum%prime !=0) {cout << f(min)<< endl; return 0;};
			if (sum%prime ==0) {sum = sum /prime; deg ++; min ++;}
		}
		//deg=a[i];
		sum++;
	}

	return 0;
}


#include <iostream>
using namespace std;
#include <cmath>
#include <string>
#include <ctime>
#include <stdlib.h>
#include <algorithm>

int prime;
int const base = 1000000007;
int f (long long deg)
{
	if (deg ==0) return 1;
	long long fun;
	fun = f (deg /2);
	fun = fun % base;
	fun = fun*fun;
	fun = fun % base;
	if (deg&1) fun *= prime;
	fun = fun % base;
	return fun;
}

long long a [111111];

int main()
{
	//freopen ("input.txt", "r", stdin); freopen ("output.txt", "w", stdout);
	int n;
	long long sum=0;
	cin >> n >> prime;
	for (int i=0; i<n; ++i)
	{
		cin >> a[i];
		sum+=a[i];
	}

	long long min=sum;
	for (int i=0; i<n; ++i){
		if ((sum-a[i])<min) min=sum-a[i];
		a[i] = sum-a[i];
	}
	for (int i=0; i<n; ++i) a[i]-=min;
	sort(a,a+n);
	int deg=0;
	sum =0;
	for (int i =0; i<n; i++)
	{
		if (a[i]==deg) {sum++; continue;}
		while (a[i]>deg)
		{
			if (sum%prime !=0) {cout << f(min)<< endl; return 0;};
			if (sum%prime ==0) {sum = sum /prime; deg++; min++;}
		}
		//deg=a[i];
		sum++;
	}
	while (sum>0) {
		if (sum%prime !=0) {cout << f(min)<< endl; return 0;};
		if (sum%prime ==0) {sum = sum /prime; deg++; min++;}
	}

	return 0;
}


#include <iostream>
using namespace std;
#include <cmath>
#include <string>
#include <ctime>
#include <stdlib.h>
#include <algorithm>

int prime;
int const base = 1000000007;
int f (int deg)
{
	if (deg ==0) return 1;
	long long fun;
	fun = f (deg /2);
	fun = fun % base;
	fun = fun*fun;
	fun = fun % base;
	if (deg&1) fun *= prime;
	fun = fun % base;
	return fun;
}

long long a [111111];

int main()
{
	//freopen ("input.txt", "r", stdin); freopen ("output.txt", "w", stdout);
	int n;
	long long sum=0;
	cin >> n >> prime;
	for (int i=0; i<n; ++i)
	{
		cin >> a[i];
		sum+=a[i];
	}

	long long min=sum;
	for (int i=0; i<n; ++i){
		if ((sum-a[i])<min) min=sum-a[i];
		a[i] = sum-a[i];
	}
	for (int i=0; i<n; ++i) a[i]-=min;
	sort(a,a+n);
	int deg=0;
	sum =0;
	a[n]=1000000010;
	for (int i =0; i<=n; i++)
	{
		if (a[i]==deg) {sum++; continue;}
		while (a[i]>deg)
		{
			if (sum%prime !=0) {cout << f(min)<< endl; return 0;};
			if (sum%prime ==0) {sum = sum /prime; deg ++; min ++;}
		}
		//deg=a[i];
		sum++;
	}

	return 0;
}


#include <iostream>
using namespace std;
#include <cmath>
#include <string>
#include <ctime>
#include <stdlib.h>
#include <algorithm>

int prime;
int const base = 1000000007;
int f (long long deg)
{
	if (deg ==0) return 1;
	long long fun;
	fun = f (deg /2);
	fun = fun % base;
	fun = fun*fun;
	fun = fun % base;
	if (deg&1) fun *= prime;
	fun = fun % base;
	return fun;
}

long long a [111111];

int main()
{
	//freopen ("input.txt", "r", stdin); freopen ("output.txt", "w", stdout);
	int n;
	long long sum=0;
	cin >> n >> prime;
	for (int i=0; i<n; ++i)
	{
		cin >> a[i];
		sum+=a[i];
	}

	long long min=sum;
	for (int i=0; i<n; ++i){
		if ((sum-a[i])<min) min=sum-a[i];
		a[i] = sum-a[i];
	}
	for (int i=0; i<n; ++i) a[i]-=min;
	sort(a,a+n);
	int deg=0;
	sum =0;
	a[n]=1000000010;
	for (int i =0; i<=n; i++)
	{
		if (a[i]==deg) {sum++; continue;}
		while (a[i]>deg)
		{
			if (sum%prime !=0) {cout << f(min)<< endl; return 0;};
			if (sum%prime ==0) {sum = sum /prime; deg ++; min ++;}
		}
		//deg=a[i];
		sum++;
	}

	return 0;
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<vector>
#include<iostream>
using namespace std;
const int N = 100005;
const int MOD = 1000000007;
typedef __int64 ll;
ll mul(ll x,ll k)
{
    ll r=1;
    while(k)
    {
        if(k%2)r=r*x%MOD;
        k>>=1;
        x=x*x%MOD;
    }
    return r;
}
int a[N],cnt[N];
int main()
{
    int n,k,x,i,mx=0,fg=0;
    ll sum=0;
    scanf("%d%d",&n,&x);
    for(i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        if(mx<a[i])mx=a[i];
        sum+=a[i];
    }
    ll r=mul(x,sum-mx);
    //printf("sum:%I64d mx:%d r:%I64d \n",sum,mx,r);
    for(i=1;i<=n;i++)a[i]=mx-a[i];
    sort(a+1,a+n+1);
    int j,pt=0,xd=mx,ct=0;
    for(i=1;i<=n&&xd;)
    {
        for(j=i;j<=n&&a[j]==a[i];j++);
        ct=j-i+pt;
        if(ct%x==0)
        {
            r=r*x%MOD;
            ct/=x;
            xd--;
            int d=1;
            if(j<=n)
            {
                while(a[i]+d<a[j]&&ct%x==0&&xd)
                {
                    ct/=x;
                    r=r*x%MOD;
                    xd--;
                    d++;
                }
                if(a[i]+d<a[j])break;
            }
            pt=ct;
        }
        else break;
        i=j;
    }
    if(j==n+1)
    {
        while(ct%x==0&&xd){r=r*x%MOD;ct/=x;xd--;}
    }
    printf("%I64d\n",r);
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<vector>
#include<iostream>
using namespace std;
const int N = 100005;
const int MOD = 1000000007;
typedef __int64 ll;
ll mul(ll x,ll k)
{
    ll r=1;
    while(k)
    {
        if(k%2)r=r*x%MOD;
        k>>=1;
        x=x*x%MOD;
    }
    return r;
}
int a[N],cnt[N];
int main()
{
    int n,k,x,i,mx=0,fg=0;
    ll sum=0;
    scanf("%d%d",&n,&x);
    for(i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        if(mx<a[i])mx=a[i];
        sum+=a[i];
    }
    ll r=mul(x,sum-mx);
    //printf("sum:%I64d mx:%d r:%I64d \n",sum,mx,r);
    for(i=1;i<=n;i++)a[i]=mx-a[i];
    sort(a+1,a+n+1);
    int j,pt=0;
    for(i=1;i<=n;)
    {
        for(j=i;j<=n&&a[j]==a[i];j++);
        int ct=j-i+pt;
        if(ct%x==0)
        {
            r=r*x%MOD;
            ct/=x;
            int d=1;
            if(j<=n)
            {
                while(a[i]+d<a[j]&&ct%x==0)
                {
                    ct/=x;
                    r=r*x%MOD;
                    d++;
                }
                if(a[i]+d<a[j])break;
            }
            pt=ct;
        }
        else break;
        i=j;
    }
    printf("%I64d\n",r);
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<vector>
#include<iostream>
using namespace std;
const int N = 100005;
const int MOD = 1000000007;
typedef __int64 ll;
ll mul(ll x,ll k)
{
    ll r=1;
    while(k)
    {
        if(k%2)r=r*x%MOD;
        k>>=1;
        x=x*x%MOD;
    }
    return r;
}
int a[N],cnt[N];
int main()
{
    int n,k,x,i,mx=0,fg=0;
    ll sum=0;
    scanf("%d%d",&n,&x);
    for(i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        if(mx<a[i])mx=a[i];
        sum+=a[i];
    }
    ll r=mul(x,sum-mx);
    //printf("sum:%I64d mx:%d r:%I64d \n",sum,mx,r);
    for(i=1;i<=n;i++)a[i]=mx-a[i];
    sort(a+1,a+n+1);
    int j,pt=0;
    for(i=1;i<=n;)
    {
        for(j=i;j<=n&&a[j]==a[i];j++);
        int ct=j-i+pt;
        if(ct%x==0)
        {
            r=r*x%MOD;
            ct/=x;
            pt=ct;
        }
        else break;
        i=j;
    }
    printf("%I64d\n",r);
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<vector>
#include<iostream>
using namespace std;
const int N = 100005;
const int MOD = 1000000007;
typedef __int64 ll;
ll mul(ll x,ll k)
{
    ll r=1;
    while(k)
    {
        if(k%2)r=r*x%MOD;
        k>>=1;
        x=x*x%MOD;
    }
    return r;
}
int a[N];
int main()
{
    int n,k,x,i,mx=0,fg=0;
    ll sum=0;
    scanf("%d%d",&n,&x);
    for(i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        if(mx<a[i])mx=a[i];
        if(i>1&&a[i]!=a[i-1])fg=1;
        sum+=a[i];
    }
    if(!fg)
    {
        ll r=mul(x,(ll)(n-1)*a[1]);
        ll ct=1,t=0;
        while(n%x==0&&t<a[1]){n/=x;ct*=x;t++;}
        //printf("r:%I64d ct:%I64d \n",r,ct);
        r=r*ct%MOD;
        printf("%I64d\n",r);
    }
    else
    {
        ll r=mul(x,sum-mx);
        printf("%I64d\n",r);
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<vector>
#include<iostream>
using namespace std;
const int N = 100005;
const int MOD = 1000000007;
typedef __int64 ll;
ll mul(ll x,ll k)
{
    ll r=1;
    while(k)
    {
        if(k%2)r=r*x%MOD;
        k>>=1;
        x=x*x%MOD;
    }
    return r;
}
int a[N],cnt[N];
int main()
{
    int n,k,x,i,mx=0,fg=0;
    ll sum=0;
    scanf("%d%d",&n,&x);
    for(i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        if(mx<a[i])mx=a[i];
        sum+=a[i];
    }
    ll r=mul(x,sum-mx);
    //printf("sum:%I64d mx:%d r:%I64d \n",sum,mx,r);
    for(i=1;i<=n;i++)a[i]=mx-a[i];
    sort(a+1,a+n+1);
    int j,pt=0;
    for(i=1;i<=n;)
    {
        for(j=i;j<=n&&a[j]==a[i];j++);
        int ct=j-i+pt;
        if(ct%x==0)
        {
            r=r*x%MOD;
            ct/=x;
            int d=1;
            if(j<=n)
            {
                while(a[i]+d<a[j]&&ct%x==0)
                {
                    ct/=x;
                    r=r*x%MOD;
                    d++;
                }
            }
            pt=ct;
        }
        else break;
        i=j;
    }
    printf("%I64d\n",r);
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<vector>
#include<iostream>
using namespace std;
const int N = 100005;
const int MOD = 1000000007;
typedef __int64 ll;
ll mul(ll x,ll k)
{
    ll r=1;
    while(k)
    {
        if(k%2)r=r*x%MOD;
        k>>=1;
        x=x*x%MOD;
    }
    return r;
}
int a[N],cnt[N];
int main()
{
    int n,k,x,i,mx=0,fg=0;
    ll sum=0;
    scanf("%d%d",&n,&x);
    for(i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        if(mx<a[i])mx=a[i];
        sum+=a[i];
    }
    ll r=mul(x,sum-mx);
    for(i=1;i<=n;i++)a[i]=mx-a[i];
    sort(a+1,a+n+1);
    int d=a[n]-a[1];
    ll tx=1;
    while(d&&tx<=10*N)
    {
        tx*=x;
        d--;
    }
    if(tx>10*N){printf("%I64d\n",r);return 0;}
    ll tot=1;
    for(i=2;i<=n;i++)
    {
        int j,tx=1;
        for(j=0;j<a[i]-a[1];j++,tx*=x);
        tot+=tx;
    }
    ll ct=1;
    while(tot%x==0){tot/=x;ct=ct*x%MOD;}
    r=r*ct%MOD;
    printf("%I64d\n",r);
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<vector>
#include<iostream>
using namespace std;
const int N = 100005;
const int MOD = 1000000007;
typedef __int64 ll;
ll mul(ll x,ll k)
{
    ll r=1;
    while(k)
    {
        if(k%2)r=r*x%MOD;
        k>>=1;
        x=x*x%MOD;
    }
    return r;
}
int a[N],cnt[N];
int main()
{
    int n,k,x,i,mx=0,fg=0;
    ll sum=0;
    scanf("%d%d",&n,&x);
    for(i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        if(mx<a[i])mx=a[i];
        sum+=a[i];
    }
    ll r=mul(x,sum-mx);
    for(i=1;i<=n;i++)a[i]=mx-a[i];
    sort(a+1,a+n+1);
    int d=a[n]-a[1];
    ll tx=1;
    while(d&&tx<=N)
    {
        tx*=x;
        d--;
    }
    if(tx>N){printf("%I64d\n",r);return 0;}
    ll tot=1;
    for(i=2;i<=n;i++)
    {
        int j,tx=1;
        for(j=0;j<a[i]-a[1];j++,tx*=x);
        tot+=tx;
    }
    ll ct=1;
    while(tot%x==0){tot/=x;ct=ct*x%MOD;}
    r=r*ct%MOD;
    printf("%I64d\n",r);
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<vector>
#include<iostream>
using namespace std;
const int N = 100005;
const int MOD = 1000000007;
typedef __int64 ll;
ll mul(ll x,ll k)
{
    ll r=1;
    while(k)
    {
        if(k%2)r=r*x%MOD;
        k>>=1;
        x=x*x%MOD;
    }
    return r;
}
int a[N],cnt[N];
int main()
{
    int n,k,x,i,mx=0,fg=0;
    ll sum=0;
    scanf("%d%d",&n,&x);
    for(i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        if(mx<a[i])mx=a[i];
        sum+=a[i];
    }
    ll r=mul(x,sum-mx);
    //printf("sum:%I64d mx:%d r:%I64d \n",sum,mx,r);
    for(i=1;i<=n;i++)a[i]=mx-a[i];
    sort(a+1,a+n+1);
    int j,pt=0,xd=mx;
    for(i=1;i<=n&&xd;)
    {
        for(j=i;j<=n&&a[j]==a[i];j++);
        int ct=j-i+pt;
        if(ct%x==0)
        {
            r=r*x%MOD;
            ct/=x;
            xd--;
            int d=1;
            if(j<=n)
            {
                while(a[i]+d<a[j]&&ct%x==0&&xd)
                {
                    ct/=x;
                    r=r*x%MOD;
                    xd--;
                    d++;
                }
                if(a[i]+d<a[j])break;
            }
            pt=ct;
        }
        else break;
        i=j;
    }
    printf("%I64d\n",r);
    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <functional>
#include <algorithm>

#define MAX 100010
#define MOD 1000000007

using namespace std;

typedef __int64 i64;

i64 a[MAX], b[MAX];

i64 modPow(i64 a, i64 b, i64 p) {
    i64 ret = 1;
    for (a %= p; b; b >>= 1) {
        if (b & 1) ret = (ret * a) % p;
        a = (a * a) % p;
    }
    return ret;
}

i64 calc(i64 n, i64 p) {
    i64 cnt;
    for (cnt = 0; !(n % p); n /= p) ++cnt;
    return cnt;
}

i64 solve(i64 *a, i64 p, int &n) {
    int x, y, t, i;
    i64 ret;

    if (!n) return 0;
    sort(a, a + n);
    x = upper_bound(a, a + n, 0) - a;
    if (x == n) {
        ret = calc(x, p); n = 0;
    } else {
        ret = y = min(calc(x, p), a[x]);

        for (t = x, i = 0; i < y; ++i) t /= p;
        for (i = x; i < n; ++i) a[i] -= y;
        sort(a, a + n, greater<i64>());
        n -= x - t;
    }

    return ret;
}

int main() {
    int n, x, i, p;
    i64 sum, cur;

    while (cin >> n >> x) {
        for (sum = i = 0; i < n; ++i) {
            cin >> a[i];
            sum += a[i];
        }
        sort(a, a + n);
        sum -= a[n - 1];
        for (i = 0; i < n; ++i) a[i] = a[n - 1] - a[i];
        while (cur = solve(a, x, n)) sum += cur;

        cout << modPow(x, sum, MOD) << endl;
    }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <functional>
#include <algorithm>

#define MAX 100010
#define MOD 1000000007

using namespace std;

typedef __int64 i64;

i64 a[MAX], b[MAX];

i64 modPow(i64 a, i64 b, i64 p) {
    i64 ret = 1;
    for (a %= p; b; b >>= 1) {
        if (b & 1) ret = (ret * a) % p;
        a = (a * a) % p;
    }
    return ret;
}

i64 calc(i64 n, i64 p) {
    i64 cnt;
    for (cnt = 0; !(n % p); n /= p) ++cnt;
    return cnt;
}

i64 solve(i64 *a, i64 p, int &n) {
    int x, y, t, i;
    i64 ret;

    if (!n) return 0;
    sort(a, a + n);
    x = upper_bound(a, a + n, 0) - a;
    if (x == n) {
        ret = calc(x, p); n = 0;
    } else {
        ret = y = min(calc(x, p), a[x]);

        for (t = x, i = 0; i < y; ++i) t /= p;
        for (i = x; i < n; ++i) a[i] -= y;
        sort(a, a + n, greater<i64>());
        n -= x - t;
    }

    return ret;
}

int main() {
    int n, x, i, p;
    i64 sum, cur, h;

    while (cin >> n >> x) {
        for (sum = i = 0; i < n; ++i) {
            cin >> a[i];
            sum += a[i];
        }
        sort(a, a + n);
        h = sum; sum -= a[n - 1];
        for (i = 0; i < n; ++i) a[i] = a[n - 1] - a[i];
        while (cur = solve(a, x, n)) sum += cur;
        if (sum > h) sum = h;

        cout << modPow(x, sum, MOD) << endl;
    }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

#define MAX 100010
#define MOD 1000000007

using namespace std;

typedef __int64 i64;

i64 a[MAX];

i64 modPow(i64 a, i64 b, i64 p) {
    i64 ret = 1;
    for (a %= p; b; b >>= 1) {
        if (b & 1) ret = (ret * a) % p;
        a = (a * a) % p;
    }
    return ret;
}

i64 calc(i64 n, i64 p) {
    i64 cnt;
    for (cnt = 0; !(n % p); n /= p) ++cnt;
    return cnt;
}

int main() {
    int n, x, i;
    i64 sum;

    while (cin >> n >> x) {
        for (sum = i = 0; i < n; ++i) {
            cin >> a[i];
            sum += a[i];
        }
        sort(a, a + n);
        sum -= a[n - 1];
        if (a[0] == a[n - 1]) sum += min(a[n - 1], calc(n, x));
        cout << modPow(x, sum, MOD) << endl;
    }

    return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

#define MAX 100010
#define MOD 1000000007

using namespace std;

typedef __int64 i64;

i64 a[MAX];

i64 modPow(i64 a, i64 b, i64 p) {
    i64 ret = 1;
    for (a %= p; b; b >>= 1) {
        if (b & 1) ret = (ret * a) % p;
        a = (a * a) % p;
    }
    return ret;
}

i64 calc(i64 n, i64 p) {
    i64 cnt;
    for (cnt = 0; !(n % p); n /= p) ++cnt;
    return cnt;
}

int main() {
    int n, x, i;
    i64 sum;

    while (cin >> n >> x) {
        for (sum = i = 0; i < n; ++i) {
            cin >> a[i];
            sum += a[i];
        }
        sort(a, a + n);
        sum -= a[n - 1];
        if (a[0] == a[n - 1]) sum += calc(n, x);
        cout << modPow(x, sum, MOD) << endl;
    }

    return 0;
}


# include <iostream>
# include <math.h>
# include <fstream>
# include <vector>
# include <algorithm>
# include <string>
using namespace std;
//printf ("%.8lf\n", a);
// erase

/*
    ifstream fin;
    fin.open("input.txt");
    ofstream fout;
    fout.open("output.txt");
*/

# define all(a) a.begin(), a.end()
# define Sort(a) sort(all(a));
# define pause system("pause");
# define ll long long
# define VI vector <int>
# define vll vector <ll>
# define pb push_back
# define pob pop_back
# define Graph vector < vector <bool> >

int mod=1000000007;

ll binpow (ll a, ll n) {
    if (n == 0)
        return 1;
    if (n % 2 == 1)
        return  (binpow (a, n-1) * a)%mod;
    else {
        ll b =  binpow (a, n/2) ;
        return (b * b)%mod;
    }
}

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

    vector <ll> a(n);
    for(int i=0; i<n; ++i)
    {
        cin>>a[i];
        s+=a[i];

    }

    ll nills=0;
    ll ans=s-a[n-1];
    vector <ll> b(n);
    for(int i=0; i<n; ++i)
    {
        b[i]=a[n-1]-a[i];

        if( !b[i] ) ++nills;
    }

    ll step=a[n-1];

    while( nills%x==0 && step>0)
    {
        ++ans;
        nills/=x;
        for(int i=0; i<n; ++i) { --b[i]; if( !b[i] ) ++nills;}
        --step;

    }

    cout<<binpow(x, ans)<<endl;
    //pause;

}


#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int M = 1000000007;
#define N 100005
typedef __int64 ll;

int a[N];
ll sum,mansr;

ll go(int x,ll n)
{
	if(n==0)
		return 1;
	ll y;
	y=go(x,n/2);
	if(n%2)
		return ((y*y)%M)*x%M;
	else
		return y*y%M;
}

int main()
{
	int i,j,k,n,x;
	int mk;
	cin>>n>>x;
	for(i=0;i<n;i++)
		scanf("%d",&a[i]);
	sort(a,a+n);
	mk=a[n-1];// important
	sum=0;
	for(i=0;i<n;i++)
		sum+=a[i];
	mansr=sum-a[n-1];// x^mansr
//	printf("\n%I64d\n",mansr);
	for(i=0;i<n;i++)
		a[i]=a[n-1]-a[i];
/*	if(mk==0)// now get x^mansr
	{
		///ssssssssssss
		printf("%I64d\n",go(x,mansr));
		return 0;
	}*/
	//cout<<"a";
	int now=a[n-1];// x^now
	ll num=0;  // number of x^now or now
	for(i=n-1;i>=0;i--)
	{
		if(a[i]==now)
			num++;
		else
		{
			//ll xx=x;
			//int y=now;
				if(num%x==0)
				{
					now++;
					num/=x;
				}
				else
					break;
		//	if(y<mmin)
		//		mmin=y;
			i++;
		}
	}
		while(num%x==0)
		{
			num/=x;
			now++;
		}
			if(now>mk)
				now=mk;
		//		printf("\n%I64d %dss\n",mansr,mmin);
			mansr+=now;// get x^mansr
		//	printf("\n%d %I64dss\n",x,mansr);
		printf("%I64d\n",go(x,mansr));
		return 0;

}


#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int M = 1000000007;
#define N 100005
typedef __int64 ll;

int a[N];
ll sum,mansr;

ll go(int x,ll n)
{
	if(n==0)
		return 1;
	ll y;
	y=go(x,n/2);
	if(n%2)
		return (y*y*x)%M;
	else
		return (y*y)%M;
}

int main()
{
	int i,j,k,n,x;
	int mk;
	cin>>n>>x;
	for(i=0;i<n;i++)
		scanf("%d",&a[i]);
	sort(a,a+n);
	mk=a[n-1];// important
	sum=0;
	for(i=0;i<n;i++)
		sum+=a[i];
	mansr=sum-a[n-1];// x^mansr
//	printf("\n%I64d\n",mansr);
	for(i=0;i<n;i++)
		a[i]=a[n-1]-a[i];
	if(mk==0)// now get x^mansr
	{
		///ssssssssssss
		printf("%I64d\n",go(x,mansr));
		return 0;
	}
	//cout<<"a";
	int now=a[n-1];
	ll num=0;
	int mmin=M;
	for(i=n-1;i>=0;i--)
	{
		if(a[i]==now)
			num++;
		else
		{
			ll xx=x;
			int y=now;
			for(j=1;;j++)
			{
				if(num%xx==0)
				{
					y++;
					xx*=x;
				}
				else
					break;
			}
			if(y<mmin)
				mmin=y;
			now=a[i];
			i++;
			num=0;
		}
	}
			ll xx=x;
			int y=now;
		//	printf("%d %d ss \n",y,num);
			for(j=1;;j++)
			{
				if(num%xx==0)
				{
					y++;
					xx*=x;
				}
				else
					break;
			}
			if(y<mmin)
				mmin=y;
			//	printf("\n%I64d %dss\n",mansr,mmin);
			mansr+=mmin;// get x^mansr
		//	printf("\n%d %I64dss\n",x,mansr);
		printf("%I64d\n",go(x,mansr));
		return 0;

}


#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int M = 1000000007;
#define N 100005
typedef __int64 ll;

int a[N];
ll sum,mansr;

ll go(int x,ll n)
{
	if(n==0)
		return 1;
	ll y;
	y=go(x,n/2);
	if(n%2)
		return ((y*y)%M)*x%M;
	else
		return y*y%M;
}

int main()
{
	int i,j,k,n,x;
	int mk;
	cin>>n>>x;
	for(i=0;i<n;i++)
		scanf("%d",&a[i]);
	sort(a,a+n);
	mk=a[n-1];// important
	sum=0;
	for(i=0;i<n;i++)
		sum+=a[i];
	mansr=sum-a[n-1];// x^mansr
	for(i=0;i<n;i++)
		a[i]=a[n-1]-a[i];
	int now=a[n-1];// x^now
	ll num=0;  // number of x^now or now
	for(i=n-1;i>=0;i--)
	{
		if(a[i]==now)
			num++;
		else
		{
				if(num%x==0)
				{
					now++;
					num/=x;
				}
				else
					break;
			i++;
		}
	}
		while(num%x==0)
		{
			num/=x;
			now++;
		}
			if(now>mk)
				now=mk;
			mansr+=now;// get x^mansr
		printf("%I64d\n",go(x,mansr));
		return 0;

}


#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int M = 1000000007;
#define N 100005
typedef unsigned __int64 ll;

int a[N];
ll sum,mansr;

ll go(int x,ll n)
{
	if(n==0)
		return 1;
	ll y;
	y=go(x,n/2);
	if(n%2)
		return (y*y*x)%M;
	else
		return (y*y)%M;
}

int main()
{
	int i,j,k,n,x;
	int mk;
	cin>>n>>x;
	for(i=0;i<n;i++)
		scanf("%d",&a[i]);
	sort(a,a+n);
	mk=a[n-1];// important
	sum=0;
	for(i=0;i<n;i++)
		sum+=a[i];
	mansr=sum-a[n-1];// x^mansr
//	printf("\n%I64d\n",mansr);
	for(i=0;i<n;i++)
		a[i]=a[n-1]-a[i];
	if(mk==0)// now get x^mansr
	{
		///ssssssssssss
		printf("%I64d\n",go(x,mansr));
		return 0;
	}
	//cout<<"a";
	int now=a[n-1],num=0;
	int mmin=M;
	for(i=n-1;i>=0;i--)
	{
		if(a[i]==now)
			num++;
		else
		{
			ll xx=x;
			int y=now;
			for(j=1;;j++)
			{
				if(!((ll)num%xx))
				{
					y++;
					xx*=x;
				}
				else
					break;
			}
			if(y<mmin)
				mmin=y;
			now=a[i];
			i++;
			num=0;
		}
	}
			ll xx=x;
			int y=now;
		//	printf("%d %d ss \n",y,num);
			for(j=1;;j++)
			{
				if(!((ll)num%xx))
				{
					y++;
					xx*=x;
				}
				else
					break;
			}
			if(y<mmin)
				mmin=y;
			//	printf("\n%I64d %dss\n",mansr,mmin);
			mansr+=mmin;// get x^mansr
		//	printf("\n%d %I64dss\n",x,mansr);
		printf("%I64d\n",go(x,mansr));
		return 0;

}


#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int M = 1000000007;
#define N 100005
typedef __int64 ll;

int a[N];
ll sum,mansr;

ll go(int x,ll n)
{
	if(n==0)
		return 1;
	ll y;
	y=go(x,n/2);
	if(n%2)
		return (y*y*x)%M;
	else
		return (y*y)%M;
}

int main()
{
	int i,j,k,n,x;
	int mk;
	cin>>n>>x;
	for(i=0;i<n;i++)
		scanf("%d",&a[i]);
	sort(a,a+n);
	mk=a[n-1];// important
	sum=0;
	for(i=0;i<n;i++)
		sum+=a[i];
	mansr=sum-a[n-1];// x^mansr
//	printf("\n%I64d\n",mansr);
	for(i=0;i<n;i++)
		a[i]=a[n-1]-a[i];
/*	if(mk==0)// now get x^mansr
	{
		///ssssssssssss
		printf("%I64d\n",go(x,mansr));
		return 0;
	}*/
	//cout<<"a";
	int now=a[n-1];
	ll num=0;
	int mmin=M;
	for(i=n-1;i>=0;i--)
	{
		if(a[i]==now)
			num++;
		else
		{
			ll xx=x;
			int y=now;
			for(j=1;;j++)
			{
				if(num%xx==0)
				{
					y++;
					xx*=x;
				}
				else
					break;
			}
			if(y<mmin)
				mmin=y;
			now=a[i];
			i++;
			num=0;
		}
	}
			ll xx=x;
			int y=now;
		//	printf("%d %d ss \n",y,num);
			for(j=1;;j++)
			{
				if(num%xx==0)
				{
					y++;
					xx*=x;
				}
				else
					break;
			}
			if(y<mmin)
				mmin=y;
			if(mmin>mk)
				mmin=mk;
			//	printf("\n%I64d %dss\n",mansr,mmin);
			mansr+=mmin;// get x^mansr
		//	printf("\n%d %I64dss\n",x,mansr);
		printf("%I64d\n",go(x,mansr));
		return 0;

}


#include <iostream>

using namespace std;

long long  maxCon(int n)
{
    return n * (n - 1) / 2;
}

int main(int argc, char const *argv[])
{
    const long MAX = 100001;
    long n, m, v;
    long long max = -1;
    long maxI = -1;

    cin >> n >> m >> v;

    if (m < n - 1 || m > maxCon(n-1) + 1)
    {
        cout << -1;
        return 0;
    }

    long left = m;

    for (int i = 1; i <= n - 1; i++)
    {
        cout << i << " " << i + 1 << endl;
        left--;
    }

    int r = (v == n) ? v - 1 : v + 1;

    for (int i = 1; left > 0 && i <= n-1; i++)
    for (int j = i + 2;  left > 0 && j <= n; j++)
    {
        if (i != r && j != r)
        {
            cout << i << " " << j << endl;
            left--;
        }

    }

    // if (left > 0)
    // {
    //     cout << "More connections left: " << left << endl;
    // }

    return 0;
}


#include <iostream>

using namespace std;

long long  maxCon(int n)
{
    return n * (n - 1) / 2;
}

int main(int argc, char const *argv[])
{
    const long MAX = 100001;
    long n, m, v;
    long long max = -1;
    long maxI = -1;

    cin >> n >> m >> v;

    if (m < n - 1 || m > maxCon(n-1) + 1)
    {
        cout << -1;
        return 0;
    }

    long left = m;
    int r = (v == n) ? v - 1 : v + 1;

    for (int i = 1; i <= n - 1; i++)
    {
        if (i != r)
        {
            cout << i << " " << i + 1 << endl;
            left--;
        }
        else
        {
            cout << v << " " << i + 1 << endl;
            left--;
        }

    }

    for (int i = 1; left > 0 && i <= n-1; i++)
    for (int j = i + 2;  left > 0 && j <= n; j++)
    {
        if (i != r && j != r)
        {
            cout << i << " " << j << endl;
            left--;
        }

    }

    // if (left > 0)
    // {
    //     cout << "More connections left: " << left << endl;
    // }

    return 0;
}


#include <iostream>

using namespace std;

long long  maxCon(int n)
{
    return n * (n - 1) / 2;
}

int main(int argc, char const *argv[])
{
    const long MAX = 100001;
    long n, m, v;

    cin >> n >> m >> v;

    if (m < n - 1 || m > maxCon(n-1) + 1)
    {
        cout << -1;
        return 0;
    }

    long left = m;

    long r, min = 1, max = n;

    if (v == n)
    {
        r = 1;
        min = 2;
    }
    else
    {
        r = n;
        max = n - 1;
    }

    cout << v << " " << r << endl;
            left--;

    for (int i = min; i <= max - 1; i++)
    {
        cout << i << " " << i + 1 << endl;
        left--;
    }

    for (int i = min; left > 0 && i <= max-1; i++)
    for (int j = i + 2;  left > 0 && j <= max; j++)
    {
        cout << i << " " << j << endl;
        left--;
    }

    // if (left > 0)
    // {
    //     cout << "More connections left: " << left << endl;
    // }

    return 0;
}


//构造割点的图
#include <bits/stdc++.h>
using namespace std;
int n,m,v;

int main(){
    scanf("%d%d%d",&n,&m,&v);
    if((n-1)*(n-2)/2 + 1<m){
        cout<<-1<<endl;
        return 0;
    }
    cout<<1<<" "<<v<<endl;
    m--;

    for(int i=2;i<=n && m;i++,m--){
        if(i!=v) cout<<v<<" "<<i<<endl;
        else m++;
    }

    for(int i = 2;i<=n;i++){
        if(i==v) continue;
        for(int j =2;j<=n && m;j++,m--){
            if(j==i) {
                m++;
                continue;
            }
            cout<<i<<" "<<j<<endl;
        }
    }
    return 0;
}


//构造割点的图
#include <bits/stdc++.h>
using namespace std;
int n,m,v;

int main(){
    scanf("%d%d%d",&n,&m,&v);
    if((n-1)*(n-2)/2 + 1<m){
        cout<<-1<<endl;
        return 0;
    }
    cout<<1<<" "<<v<<endl;
    m--;

    for(int i=2;i<=n && m;i++,m--){
        if(i!=v) cout<<v<<" "<<i<<endl;
        else m++;
    }

    for(int i = 2;i<=n;i++){
        if(i==v) continue;
        for(int j =2;j<=n && m;j++,m--){
            if(j==i || j == v) {
                m++;
                continue;
            }
            cout<<i<<" "<<j<<endl;
        }
    }
    return 0;
}


//构造割点的图
#include <bits/stdc++.h>
using namespace std;
int n,m,v;

int main(){
    scanf("%d%d%d",&n,&m,&v);
    if((n-1)*(n-2)/2 + 1<m){
        cout<<-1<<endl;
        return 0;
    }
    int t;
    if(v==1) t=2;
    else t =1;
    cout<<v<<" "<<t<<endl;
    m--;
    for(int i =1;i<=n;i++){
        if(i==v) continue;
        for(int j = i+1;j<=n && m;j++,m--){
            if(j==v) {
                m++;continue;
            }else{
                cout<<i<<" "<<j<<endl;
            }
        }
    }

    return 0;
}


//构造割点的图
#include <bits/stdc++.h>
using namespace std;
long long n;
int m,v;

int main(){
    scanf("%lld%d%d",&n,&m,&v);
    if((n-1)*(n-2)/2 + 1<m){
        cout<<-1<<endl;
        return 0;
    }
    int t;
    if(v==1) t=2;
    else t =1;
    cout<<v<<" "<<t<<endl;
    m--;
    for(int i =1;i<=n;i++){
        if(i==t) continue;
        for(int j = i+1;j<=n && m;j++,m--){
            if(j==t) {
                m++;continue;
            }else{
                cout<<i<<" "<<j<<endl;
            }
        }
    }

    return 0;
}


//构造割点的图
#include <bits/stdc++.h>
using namespace std;
long long n;
int m,v;

int main(){
    scanf("%lld%d%d",&n,&m,&v);
    long long res = (n-1)*(n-2)/2 + 1;
    if(res <m || m<n-1){
        cout<<-1<<endl;
        return 0;
    }
    int t;
    if(v==1) t=2;
    else t =1;
    cout<<v<<" "<<t<<endl;
    m--;
    for(int i =1;i<=n;i++){
        if(i==t) continue;
        for(int j = i+1;j<=n && m;j++,m--){
            if(j==t) {
                m++;continue;
            }else{
                cout<<i<<" "<<j<<endl;
            }
        }
    }

    return 0;
}


//构造割点的图
#include <bits/stdc++.h>
using namespace std;
int n,m,v;

int main(){
    scanf("%d%d%d",&n,&m,&v);
    if((n-1)*(n-2)/2 + 1<m){
        cout<<-1<<endl;
        return 0;
    }
    int t;
    if(v==1) t=2;
    else t =1;
    cout<<v<<" "<<t<<endl;
    m--;
    for(int i =1;i<=n;i++){
        if(i==t) continue;
        for(int j = i+1;j<=n && m;j++,m--){
            if(j==t) {
                m++;continue;
            }else{
                cout<<i<<" "<<j<<endl;
            }
        }
    }

    return 0;
}


//============================================================================
// Name           : Riham.cpp
// Author         : Remo
// Description   : Hello World in C++, Ansi-style
//============================================================================
#include <bits/stdc++.h>
using namespace std;

typedef	long long	ll;
typedef	long double	ld;
#define	Set(v,d)	memset(v, d, sizeof(v))
#define	oo		100000000   //infinity
#define F		first
#define S		second
#define	pb		push_back
#define	sz(x) 	(int)(x.size())
#define	all(x)		(x.begin()),(x.end())
#define	rall(x)		(x.rbegin()),(x.rend())

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
#endif
	int n, m, v;
	scanf("%d%d%d", &n, &m, &v);
	if( (n * (n-1) )/2 < m){
		puts("-1");
		return 0;
	}
	for(int i=1;i<=n;i++){
		if(i == v) continue;
		printf("%d %d\n", v, i);
	}
	m-=(n-1);
	for(int i=1;i<=n;i++){
		if(i == v) continue;
		for(int j=i+1;j<=n;j++){
			if(j == v) continue;
			if(m<=0) return 0;
			if(i != j)
				printf("%d %d\n", i, j);
			m--;
		}
	}

	return 0;
}


//============================================================================
// Name           : Riham.cpp
// Author         : Remo
// Description   : Hello World in C++, Ansi-style
//============================================================================
#include <bits/stdc++.h>
using namespace std;

typedef	long long	ll;
typedef	long double	ld;
#define	Set(v,d)	memset(v, d, sizeof(v))
#define	oo		100000000   //infinity
#define F		first
#define S		second
#define	pb		push_back
#define	sz(x) 	(int)(x.size())
#define	all(x)		(x.begin()),(x.end())
#define	rall(x)		(x.rbegin()),(x.rend())

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
#endif
	int n, m, v;
	scanf("%d%d%d", &n, &m, &v);
	if( (n * (n-1) )/2 < m || m < n - 1){
		puts("-1");
		return 0;
	}
	for(int i=1;i<=n;i++){
		if(i == v) continue;
		printf("%d %d\n", v, i);
	}
	m-=(n-1);
	for(int i=1;i<=n;i++){
		if(i == v) continue;
		for(int j=1;j<i;j++){
			if(j == v) continue;
			if(m<=0) return 0;
			if(i != j)
				printf("%d %d\n", i, j);
			m--;
		}
	}

	return 0;
}


//============================================================================
// Name           : Riham.cpp
// Author         : Remo
// Description   : Hello World in C++, Ansi-style
//============================================================================
#include <bits/stdc++.h>
using namespace std;

typedef	long long	ll;
typedef	long double	ld;
#define	Set(v,d)	memset(v, d, sizeof(v))
#define	oo		100000000   //infinity
#define F		first
#define S		second
#define	pb		push_back
#define	sz(x) 	(int)(x.size())
#define	all(x)		(x.begin()),(x.end())
#define	rall(x)		(x.rbegin()),(x.rend())

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
#endif
	int n, m, v;
	scanf("%d%d%d", &n, &m, &v);
	if( ((n-2) * (n-1) )/2 + 1 < m || m < n - 1){
		puts("-1");
		return 0;
	}
	for(int i=1;i<=n;i++){
		if(i == v) continue;
		printf("%d %d\n", v, i);
	}
	m-=(n-1);
	for(int i=1;i<=n;i++){
		if(i == v) continue;
		for(int j=1;j<i;j++){
			if(j == v) continue;
			if(m<=0) return 0;
			if(i != j)
				printf("%d %d\n", i, j);
			m--;
		}
	}

	return 0;
}


//============================================================================
// Name           : Riham.cpp
// Author         : Remo
// Description   : Hello World in C++, Ansi-style
//============================================================================
#include <bits/stdc++.h>
using namespace std;

typedef	long long	ll;
typedef	long double	ld;
#define	Set(v,d)	memset(v, d, sizeof(v))
#define	oo		100000000   //infinity
#define F		first
#define S		second
#define	pb		push_back
#define	sz(x) 	(int)(x.size())
#define	all(x)		(x.begin()),(x.end())
#define	rall(x)		(x.rbegin()),(x.rend())

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
#endif
	int n, m, v;
	scanf("%d%d%d", &n, &m, &v);
	if( (n * (n-1) )/2 < m){
		puts("-1");
		return 0;
	}
	for(int i=1;i<=n;i++){
		if(i == v) continue;
		printf("%d %d\n", v, i);
	}
	m-=(n-1);
	for(int i=1;i<=n;i++){
		if(i == v) continue;
		for(int j=1;j<i;j++){
			if(j == v) continue;
			if(!m) return 0;
			if(i != j)
				printf("%d %d\n", i, j);
			m--;
		}
	}

	return 0;
}


//============================================================================
// Name           : Riham.cpp
// Author         : Remo
// Description   : Hello World in C++, Ansi-style
//============================================================================
#include <bits/stdc++.h>
using namespace std;

typedef	long long	ll;
typedef	long double	ld;
#define	Set(v,d)	memset(v, d, sizeof(v))
#define	oo		100000000   //infinity
#define F		first
#define S		second
#define	pb		push_back
#define	sz(x) 	(int)(x.size())
#define	all(x)		(x.begin()),(x.end())
#define	rall(x)		(x.rbegin()),(x.rend())

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
#endif
	int n, m, v;
	scanf("%d%d%d", &n, &m, &v);
	if( ((n-2) * (n-1) )/2 < m || m < n - 1){
		puts("-1");
		return 0;
	}
	for(int i=1;i<=n;i++){
		if(i == v) continue;
		printf("%d %d\n", v, i);
	}
	m-=(n-1);
	for(int i=1;i<=n;i++){
		if(i == v) continue;
		for(int j=1;j<i;j++){
			if(j == v) continue;
			if(m<=0) return 0;
			if(i != j)
				printf("%d %d\n", i, j);
			m--;
		}
	}

	return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

int n,m,v;
vector <int> num;
vector <pair <int,int> > ans;

int main()
{
    scanf("%d %d %d",& n,& m,& v);

    if(m<(n-1) || ((m-(n-1))>(n-3)*(n-2)/2)){ //两个Bug
        printf("-1\n");
        return 0;
    }

    //首先要保证图连通
    for(register int i=1;i<=n;i++){
        if(i==v) continue;
        else m--,printf("%d %d\n",i,v);
    }

    //如果刚好连完了就停止程序要不然就要死循环
    if(m==0) return 0;
    int u=v+1;
    if(v==n)
        u+=2;
    for(register int i=1;i<=n;i++)
      {

        for(register int j=1;j<=n;j++)
    {
        if(i!=v&&i!=u&&j!=v&&j!=u)
        {
            m--;
            printf("%d %d\n",i,j);
            if(m==0)
                return 0;
        }
    }
      if(m==0)
                break;
      }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

int n,m,v;
vector <int> num;
vector <pair <int,int> > ans;

int main()
{
    scanf("%d %d %d",& n,& m,& v);

    if(m<(n-1) || ((m-(n-1))>(n-3)*(n-2)/2)){ //两个Bug
        printf("-1\n");
        return 0;
    }

    //首先要保证图连通
    for(register int i=1;i<=n;i++){
        if(i==v) continue;
        else m--,printf("%d %d\n",i,v);
    }

    //如果刚好连完了就停止程序要不然就要死循环
    if(m==0) return 0;
    int u=v+1;
    if(v==n)
        u+=2;
    for(register int i=1;i<=n;i++)
      {

        for(register int j=j+1;j<=n;j++)
    {
        if(i!=v&&i!=u&&j!=v&&j!=u)
        {
            m--;
            printf("%d %d\n",i,j);
            if(m==0)
                return 0;
        }
    }
      if(m==0)
                break;
      }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

int n,m,v;
vector <int> num;
vector <pair <int,int> > ans;

int main()
{
    scanf("%d %d %d",& n,& m,& v);

    if(m<(n-1) || ((m-(n-1))>(n-3)*(n-2)/2)){ //两个Bug
        printf("-1\n");
        return 0;
    }

    //首先要保证图连通
    for(register int i=1;i<=n;i++){
        if(i==v) continue;
        else m--,printf("%d %d\n",i,v);
    }

    //如果刚好连完了就停止程序要不然就要死循环
    if(m==0) return 0;
    int u=v+1;
    if(v==n)
        u-=2;
    for(register int i=1;i<=n;i++)
      {

        for(register int j=i+1;j<=n;j++)
    {
        if(i!=v&&i!=u&&j!=v&&j!=u)
        {
            m--;
            printf("%d %d\n",i,j);
            if(m==0)
                return 0;
        }
    }
      }
    return 0;
}


#define ioFix ios::sync_with_stdio(false)

#define PI (2.0 * acos(0.0))
#define INF (int)1e9
#define LINF (long long)1e18
#define EPS 1e-9

#define vi vector<int>
#define vvi vector<vector<int>>
#define vvvi vector<vector<vector<int>>>
#define ii pair<int, int>
#define vii vector<pair<int, int>>

#define vd vector<double>
#define vvd vector<vector<double>>
#define vvvd vector<vector<vector<double>>>
#define dd pair<int, int>
#define vdd vector<pair<double, double>>

#define vc vector<char>
#define vvc vector<vector<char>>
#define vvvc vector<vector<vector<char>>>
#define cc pair<int, int>
#define vcc vector<pair<char, char>>

#define vb vector<bool>
#define vvb vector<vector<bool>>
#define vvvb vector<vector<vector<bool>>>

#define vs vector<string>
#define vvs vector<vector<string>>
#define vvvs vector<vector<vector<string>>>
#define vss vector<pair<string, string>>

#define msi map<string, int>
#define msc map<string, char>
#define mss map<string, string>
#define msb map<string, bool>
#define mis map<int, string>

#define REP(n) for(int i = 0; i < (n); i++)
#define FOR(i, from, to) for(int (i) = from; (i) < (to); (i)++)
#define ALL(x) x.begin(),x.end()

#define pb push_back

#define watch(x) cout << (#x) << " is " << (x) << endl
#define watchArray(x, rows, cols) for(int r = 0; r < (rows); r++) { for(int c = 0; c < (cols); c++) { cout << (x)[r][c] << " "; } cout << endl; }

#define fillArray(arr, val) std::fill(std::begin(arr), std::end(arr), (val))

#define pow2(x) ((x)*(x))
#define mod(x, m) ((((x) % (m)) + (m)) % (m))
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))

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

// a node can have at most n-1 connections
// index (V) doesn't matter
// if m >= n(n-1) we have a complete graph i.e. impossible
// needs at least n>=3 and m>=2
// most optimal setup is having 1 lonely server, a complete cluster and those separated by the failing server.
// we can even think of this as a complete cluster and then precisely one edge going out to a lonely server.
// Maximum M for above case? M <= (n-1)(n-2)+1
int main() {
  ioFix;
  int N, M, V;
  cin >> N >> M >> V;

  // not sure if this is needed now
  if(M >= N*(N-1) || M < 2) {
    cout << -1 << endl;
    return 0;
  }

  if(M <= (N-1)*(N-2)) {
    int edgesLeft = M;
    int lonely = (V == 1);

    // probably have to start connecting from V
    for(int i = 0; i < N; i++) {
      for(int j = 0; j < N; j++) {
        if(i == j || i == lonely || j == lonely) continue;
        if(edgesLeft == 1) break;
        cout << i+1 << " " << j+1 << endl;
        edgesLeft--;
      }
      if(edgesLeft == 1) break;
    }

    cout << V << " " << lonely+1 << endl;

  } else {
    cout << -1 << endl;
  }

  return 0;
}


#define ioFix ios::sync_with_stdio(false)

#define PI (2.0 * acos(0.0))
#define INF (int)1e9
#define LINF (long long)1e18
#define EPS 1e-9

#define vi vector<int>
#define vvi vector<vector<int>>
#define vvvi vector<vector<vector<int>>>
#define ii pair<int, int>
#define vii vector<pair<int, int>>

#define vd vector<double>
#define vvd vector<vector<double>>
#define vvvd vector<vector<vector<double>>>
#define dd pair<int, int>
#define vdd vector<pair<double, double>>

#define vc vector<char>
#define vvc vector<vector<char>>
#define vvvc vector<vector<vector<char>>>
#define cc pair<int, int>
#define vcc vector<pair<char, char>>

#define vb vector<bool>
#define vvb vector<vector<bool>>
#define vvvb vector<vector<vector<bool>>>

#define vs vector<string>
#define vvs vector<vector<string>>
#define vvvs vector<vector<vector<string>>>
#define vss vector<pair<string, string>>

#define msi map<string, int>
#define msc map<string, char>
#define mss map<string, string>
#define msb map<string, bool>
#define mis map<int, string>

#define REP(n) for(int i = 0; i < (n); i++)
#define FOR(i, from, to) for(int (i) = from; (i) < (to); (i)++)
#define ALL(x) x.begin(),x.end()

#define pb push_back

#define watch(x) cout << (#x) << " is " << (x) << endl
#define watchArray(x, rows, cols) for(int r = 0; r < (rows); r++) { for(int c = 0; c < (cols); c++) { cout << (x)[r][c] << " "; } cout << endl; }

#define fillArray(arr, val) std::fill(std::begin(arr), std::end(arr), (val))

#define pow2(x) ((x)*(x))
#define mod(x, m) ((((x) % (m)) + (m)) % (m))
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))

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

// a node can have at most n-1 connections
// index (V) doesn't matter
// if m >= n(n-1) we have a complete graph i.e. impossible
// needs at least n>=3 and m>=2
// more generally, we need m >= n-1
// most optimal setup is having 1 lonely server, a complete cluster and those separated by the failing server.
// we can even think of this as a complete cluster and then precisely one edge going out to a lonely server.
// Maximum M for above case? M <= (n-1)(n-2)+1
int main() {
  ioFix;
  int N, M, V;
  cin >> N >> M >> V;

  // not sure if this is needed now
  if(M >= N*(N-1) || M < N-1) {
    cout << -1 << endl;
    return 0;
  }

  if(M <= (N-1)*(N-2)/2 + 1) {
    int edgesLeft = M;
    int lonely = (V == 1);

    // start connecting from V except lonely
    for(int i = 0; i < N; i++) {
      if(edgesLeft == 1) break;
      if(V == i+1) continue;
      if(i == lonely) continue;
      cout << V << " " << i+1 << endl;
      edgesLeft--;
    }

    // connect the rest
    for(int i = 0; i < N; i++) {
      for(int j = i+1; j < N; j++) {
        if(i+1 == V || j+1 == V) continue;
        if(i == j || i == lonely || j == lonely) continue;
        if(edgesLeft == 1) break;

        cout << i+1 << " " << j+1 << endl;
        edgesLeft--;
      }
      if(edgesLeft == 1) break;
    }

    cout << V << " " << lonely+1 << endl;

  } else {
    cout << -1 << endl;
  }

  return 0;
}


#define ioFix ios::sync_with_stdio(false)

#define PI (2.0 * acos(0.0))
#define INF (int)1e9
#define LINF (long long)1e18
#define EPS 1e-9

#define vi vector<int>
#define vvi vector<vector<int>>
#define vvvi vector<vector<vector<int>>>
#define ii pair<int, int>
#define vii vector<pair<int, int>>

#define vd vector<double>
#define vvd vector<vector<double>>
#define vvvd vector<vector<vector<double>>>
#define dd pair<int, int>
#define vdd vector<pair<double, double>>

#define vc vector<char>
#define vvc vector<vector<char>>
#define vvvc vector<vector<vector<char>>>
#define cc pair<int, int>
#define vcc vector<pair<char, char>>

#define vb vector<bool>
#define vvb vector<vector<bool>>
#define vvvb vector<vector<vector<bool>>>

#define vs vector<string>
#define vvs vector<vector<string>>
#define vvvs vector<vector<vector<string>>>
#define vss vector<pair<string, string>>

#define msi map<string, int>
#define msc map<string, char>
#define mss map<string, string>
#define msb map<string, bool>
#define mis map<int, string>

#define REP(n) for(int i = 0; i < (n); i++)
#define FOR(i, from, to) for(int (i) = from; (i) < (to); (i)++)
#define ALL(x) x.begin(),x.end()

#define pb push_back

#define watch(x) cout << (#x) << " is " << (x) << endl
#define watchArray(x, rows, cols) for(int r = 0; r < (rows); r++) { for(int c = 0; c < (cols); c++) { cout << (x)[r][c] << " "; } cout << endl; }

#define fillArray(arr, val) std::fill(std::begin(arr), std::end(arr), (val))

#define pow2(x) ((x)*(x))
#define mod(x, m) ((((x) % (m)) + (m)) % (m))
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))

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

// a node can have at most n-1 connections
// index (V) doesn't matter
// if m >= n(n-1) we have a complete graph i.e. impossible
// needs at least n>=3 and m>=2
// more generally, we need m >= n-1
// most optimal setup is having 1 lonely server, a complete cluster and those separated by the failing server.
// we can even think of this as a complete cluster and then precisely one edge going out to a lonely server.
// Maximum M for above case? M <= (n-1)(n-2)+1
int main() {
  ioFix;
  int N, M, V;
  cin >> N >> M >> V;

  // not sure if this is needed now
  if(M >= N*(N-1) || M < N-1) {
    cout << -1 << endl;
    return 0;
  }

  if(M <= (N-1)*(N-2)) {
    int edgesLeft = M;
    int lonely = (V == 1);

    // start connecting from V except lonely
    for(int i = 0; i < N; i++) {
      if(edgesLeft == 1) break;
      if(V == i+1) continue;
      if(i == lonely) continue;
      cout << V << " " << i+1 << endl;
      edgesLeft--;
    }

    // connect the rest
    for(int i = 0; i < N; i++) {
      for(int j = i+1; j < N; j++) {
        if(i+1 == V || j+1 == V) continue;
        if(i == j || i == lonely || j == lonely) continue;
        if(edgesLeft == 1) break;

        cout << i+1 << " " << j+1 << endl;
        edgesLeft--;
      }
      if(edgesLeft == 1) break;
    }

    cout << V << " " << lonely+1 << endl;

  } else {
    cout << -1 << endl;
  }

  return 0;
}


#define ioFix ios::sync_with_stdio(false)

#define PI (2.0 * acos(0.0))
#define INF (int)1e9
#define LINF (long long)1e18
#define EPS 1e-9

#define vi vector<int>
#define vvi vector<vector<int>>
#define vvvi vector<vector<vector<int>>>
#define ii pair<int, int>
#define vii vector<pair<int, int>>

#define vd vector<double>
#define vvd vector<vector<double>>
#define vvvd vector<vector<vector<double>>>
#define dd pair<int, int>
#define vdd vector<pair<double, double>>

#define vc vector<char>
#define vvc vector<vector<char>>
#define vvvc vector<vector<vector<char>>>
#define cc pair<int, int>
#define vcc vector<pair<char, char>>

#define vb vector<bool>
#define vvb vector<vector<bool>>
#define vvvb vector<vector<vector<bool>>>

#define vs vector<string>
#define vvs vector<vector<string>>
#define vvvs vector<vector<vector<string>>>
#define vss vector<pair<string, string>>

#define msi map<string, int>
#define msc map<string, char>
#define mss map<string, string>
#define msb map<string, bool>
#define mis map<int, string>

#define REP(n) for(int i = 0; i < (n); i++)
#define FOR(i, from, to) for(int (i) = from; (i) < (to); (i)++)
#define ALL(x) x.begin(),x.end()

#define pb push_back

#define watch(x) cout << (#x) << " is " << (x) << endl
#define watchArray(x, rows, cols) for(int r = 0; r < (rows); r++) { for(int c = 0; c < (cols); c++) { cout << (x)[r][c] << " "; } cout << endl; }

#define fillArray(arr, val) std::fill(std::begin(arr), std::end(arr), (val))

#define pow2(x) ((x)*(x))
#define mod(x, m) ((((x) % (m)) + (m)) % (m))
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))

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

// a node can have at most n-1 connections
// index (V) doesn't matter
// if m >= n(n-1) we have a complete graph i.e. impossible
// needs at least n>=3 and m>=2
// more generally, we need m >= n-1
// most optimal setup is having 1 lonely server, a complete cluster and those separated by the failing server.
// we can even think of this as a complete cluster and then precisely one edge going out to a lonely server.
// Maximum M for above case? M <= (n-1)(n-2)+1
int main() {
  ioFix;
  int N, M, V;
  cin >> N >> M >> V;

  // not sure if this is needed now
  if(M >= N*(N-1) || M < N-1) {
    cout << -1 << endl;
    return 0;
  }

  if(M <= (N-1)*(N-2)/2) {
    int edgesLeft = M;
    int lonely = (V == 1);

    // start connecting from V except lonely
    for(int i = 0; i < N; i++) {
      if(edgesLeft == 1) break;
      if(V == i+1) continue;
      if(i == lonely) continue;
      cout << V << " " << i+1 << endl;
      edgesLeft--;
    }

    // connect the rest
    for(int i = 0; i < N; i++) {
      for(int j = i+1; j < N; j++) {
        if(i+1 == V || j+1 == V) continue;
        if(i == j || i == lonely || j == lonely) continue;
        if(edgesLeft == 1) break;

        cout << i+1 << " " << j+1 << endl;
        edgesLeft--;
      }
      if(edgesLeft == 1) break;
    }

    cout << V << " " << lonely+1 << endl;

  } else {
    cout << -1 << endl;
  }

  return 0;
}


#define ioFix ios::sync_with_stdio(false)

#define PI (2.0 * acos(0.0))
#define INF (int)1e9
#define LINF (long long)1e18
#define EPS 1e-9

#define vi vector<int>
#define vvi vector<vector<int>>
#define vvvi vector<vector<vector<int>>>
#define ii pair<int, int>
#define vii vector<pair<int, int>>

#define vd vector<double>
#define vvd vector<vector<double>>
#define vvvd vector<vector<vector<double>>>
#define dd pair<int, int>
#define vdd vector<pair<double, double>>

#define vc vector<char>
#define vvc vector<vector<char>>
#define vvvc vector<vector<vector<char>>>
#define cc pair<int, int>
#define vcc vector<pair<char, char>>

#define vb vector<bool>
#define vvb vector<vector<bool>>
#define vvvb vector<vector<vector<bool>>>

#define vs vector<string>
#define vvs vector<vector<string>>
#define vvvs vector<vector<vector<string>>>
#define vss vector<pair<string, string>>

#define msi map<string, int>
#define msc map<string, char>
#define mss map<string, string>
#define msb map<string, bool>
#define mis map<int, string>

#define REP(n) for(int i = 0; i < (n); i++)
#define FOR(i, from, to) for(int (i) = from; (i) < (to); (i)++)
#define ALL(x) x.begin(),x.end()

#define pb push_back

#define watch(x) cout << (#x) << " is " << (x) << endl
#define watchArray(x, rows, cols) for(int r = 0; r < (rows); r++) { for(int c = 0; c < (cols); c++) { cout << (x)[r][c] << " "; } cout << endl; }

#define fillArray(arr, val) std::fill(std::begin(arr), std::end(arr), (val))

#define pow2(x) ((x)*(x))
#define mod(x, m) ((((x) % (m)) + (m)) % (m))
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))

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

// a node can have at most n-1 connections
// index (V) doesn't matter
// if m >= n(n-1) we have a complete graph i.e. impossible
// needs at least n>=3 and m>=2
// most optimal setup is having 1 lonely server, a complete cluster and those separated by the failing server.
// we can even think of this as a complete cluster and then precisely one edge going out to a lonely server.
// Maximum M for above case? M <= (n-1)(n-2)+1
int main() {
  ioFix;
  int N, M, V;
  cin >> N >> M >> V;

  // not sure if this is needed now
  if(M >= N*(N-1) || M < 2) {
    cout << -1 << endl;
    return 0;
  }

  if(M <= (N-1)*(N-2)) {
    int edgesLeft = M;
    int lonely = (V == 1);

    // start connecting from V except lonely
    for(int i = 0; i < N; i++) {
      if(edgesLeft == 1) break;
      if(V == i+1) continue;
      if(i == lonely) continue;
      cout << V << " " << i+1 << endl;
      edgesLeft--;
    }

    // connect the rest
    for(int i = 0; i < N; i++) {
      for(int j = i+1; j < N; j++) {
        if(i+1 == V || j+1 == V) continue;
        if(i == j || i == lonely || j == lonely) continue;
        if(edgesLeft == 1) break;

        cout << i+1 << " " << j+1 << endl;
        edgesLeft--;
      }
      if(edgesLeft == 1) break;
    }

    cout << V << " " << lonely+1 << endl;

  } else {
    cout << -1 << endl;
  }

  return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;

int n, m, v;

void input(){
    cin >> n >> m >> v;
}

void process(){
    int canh = n - 1;
    if( m < n - 1 || m > ((n - 2) * (n - 3) + 1 ) ){
        cout << "-1";
        return;
    }
    for(int i = 1; i <= n; i++){
        if( i != v ) cout << i << " " << v << endl;
    }
    int flag = 0;
    int s = 1;
    if( v == 1 ) s = 2;
    int cnt = n - 1;
    for(int i = 1; i <= n; i++){
        for(int j = i + 1; j <= n; j++){
            if( i == v || i == s || j == v || j == s )
                continue;
            if( cnt == m ){
                flag = 1;
                break;
            }
            cout << i << " " << j << endl;
            cnt++;
        }
        if( flag ) break;
    }
}

int main(void){
    //freopen("inp.txt", "r", stdin);
    //freopen("out.txt","w",stdout);
    input();
    process();
    return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;

int n, m, v;

void input(){
    cin >> n >> m >> v;
}

void process(){
    int canh = n - 1;
    if( m < n - 1 || m > (((n-1) * (n-2) / 2 ) + 1) ){
        cout << "-1";
        return;
    }
    for(int i = 1; i <= n; i++){
        if( i != v ) cout << i << " " << v << endl;
    }
    int flag = 0;
    int k = n;
    for(int i = 1; i <= n; i++){
        if( i != v ){
            int dem = 1;
            for(int j = i + 1; j <= k; j++){
                if( j != v ){
                    cout << i << " " << j << endl;
                    canh++;
                    dem++;
                    if( canh == m ) {
                        flag = 1;
                        break;
                    }
                    if( dem == n - 2 ) {
                        break;
                    }
                }
            }
            k--;
        }
        if( flag ) break;
    }
}

int main(void){
    //freopen("inp.txt", "r", stdin);
    //freopen("out.txt","w",stdout);
    input();
    process();
    return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;

int n, m, v;

void input(){
    cin >> n >> m >> v;
}

void process(){
    int canh = n - 1;
    if( m < n - 1 || m > (((n-1) * (n-2) / 2 ) + 1) ){
        cout << "-1";
        return;
    }
    for(int i = 1; i <= n; i++){
        if( i != v ) cout << i << " " << v << endl;
    }
    int flag = 0;
    for(int i = 1; i <= n; i++){
        if( i != v ){
            int dem = 1;
            for(int j = i + 1; j <= n; j++){
                if( j != v ){
                    cout << i << " " << j << endl;
                    canh++;
                    dem++;
                    if( canh == m ) {
                        flag = 1;
                        break;
                    }
                    if( dem == n - 2 ) {
                        break;
                    }
                }
            }
        }
        if( flag ) break;
    }
}

int main(void){
    //freopen("inp.txt", "r", stdin);
    input();
    process();
    return 0;
}


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;

int n, m, v;

void input(){
    cin >> n >> m >> v;
}

void process(){
    int canh = n - 1;
    if( m < n - 1 || m > (((n-1) * (n-2) / 2 ) + 1) ){
        cout << "-1";
        return;
    }
    for(int i = 1; i <= n; i++){
        if( i != v ) cout << i << " " << v << endl;
    }
    int flag = 0;
    int s = 1;
    if( v == 1 ) s = 2;
    int cnt = n - 1;
    for(int i = 1; i <= n; i++){
        for(int j = i + 1; j <= n; j++){
            if( i == v || i == s || j == v || j == s )
                continue;
            if( cnt == m ){
                flag = 1;
                break;
            }
            cout << i << " " << j << endl;
            cnt++;
        }
        if( flag ) break;
    }
}

int main(void){
    //freopen("inp.txt", "r", stdin);
    //freopen("out.txt","w",stdout);
    input();
    process();
    return 0;
}


#include <iostream>
using namespace std;

int main (){
    int n,m,v;
    cin >> n>>m>>v;
    int sd = (((n-1)*(n-2))/2)+1;
    if( sd < m) cout <<-1;
    else {
        int dem=0;
            for (int i=1;i<n;i++){
                if (i != v){
                    for (int j=i+1;j<n;j++){
                        if (dem != m-(n-1)){
                            if (j!=v){
                                cout << i<<" "<<j<<endl;
                                dem++;
                            }
                        }
                        else break;
                    }
                }
                else {
                    for (int i=1;i<=n;i++){
                        if (i<v && i!=v){
                            cout << i << " " << v <<endl;
                        }
                        else if (i>v && i!= v){
                            cout << v << " " <<i<<endl;
                        }
                    }
                }
            }
        }

}


//iN tHe nAMe oF gOd
using namespace std ;
#include<bits/stdc++.h>
#include<cstdio>
#include<iostream>
#define int long long
#define ld long double
#define pld pair<ld , ld>
#define F first
#define S second
#define cerr cout << "*" << endl
#define pii pair <int , int>
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define vi vector <int>
#define si set<int>
#define msi multiset <int>
#define qi queue<int>
#define ashari cout << setprecision(8)
// #define cin fin
// #define cout fout
const int mod = 1e9 + 7 ;
const int inf = 1e18 ;
const int aval = 210 ;
int n , m , x ;

main()
{
  fast ;
  cin >> n >> m >> x ;
  if(m < n - 1 || m > ((n * (n - 1)) / 2) - n + 1)
    {
      cout << -1 ;
      return 0 ;
    }
  for(int i = 1 ; i <= n ; i ++)
    if(i != x)
      cout << i << " "  << x << endl ;
  m -= n -1 ;
  int i = 1 ;
  int f = n ;
  if(f == x)
    f -- ;
  while(m)
    {
      if(i == x)
	i ++ ;
      for(int j = i + 1 ; j < n && m; j ++)
	{
	  if(j != f)
	    {
	  cout << i << " " << j << endl ;
	  m -- ;
	    }
	}
      i ++ ;
    }
}


//iN tHe nAMe oF gOd
using namespace std ;
#include<bits/stdc++.h>
#include<cstdio>
#include<iostream>
#define int long long
#define ld long double
#define pld pair<ld , ld>
#define F first
#define S second
#define cerr cout << "*" << endl
#define pii pair <int , int>
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define vi vector <int>
#define si set<int>
#define msi multiset <int>
#define qi queue<int>
#define ashari cout << setprecision(8)
// #define cin fin
// #define cout fout
const int mod = 1e9 + 7 ;
const int inf = 1e18 ;
const int aval = 210 ;
int n , m , x ;

main()
{
  fast ;
  cin >> n >> m >> x ;
  if(m < n - 1 || m > ((n * (n - 1)) / 2) - n + 1)
    {
      cout << -1 ;
      return 0 ;
    }
  for(int i = 1 ; i <= n ; i ++)
    if(i != x)
      cout << i << " "  << x << endl ;
  m -= n -1 ;
  int i = 1 ;
  while(m)
    {
      if(i == x)
	i ++ ;
      for(int j = i + 1 ; j < n && m; j ++)
	{
	  if(j != x)
	    {
	  cout << i << " " << j << endl ;
	  m -- ;
	    }
	}
      i ++ ;
    }
}


//iN tHe nAMe oF gOd
using namespace std ;
#include<bits/stdc++.h>
#include<cstdio>
#include<iostream>
#define int long long
#define ld long double
#define pld pair<ld , ld>
#define F first
#define S second
#define cerr cout << "*" << endl
#define pii pair <int , int>
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define vi vector <int>
#define si set<int>
#define msi multiset <int>
#define qi queue<int>
#define ashari cout << setprecision(8)
// #define cin fin
// #define cout fout
const int mod = 1e9 + 7 ;
const int inf = 1e18 ;
const int aval = 210 ;
int n , m , x ;

main()
{
  fast ;
  cin >> n >> m >> x ;
  if(m < n - 1 || m > (n * (n - 1)) / 2)
    {
      cout << -1 ;
      return 0 ;
    }
  for(int i = 1 ; i <= n ; i ++)
    if(i != x)
      cout << i << " "  << x << endl ;
  m -= n -1 ;
  int i = 1 ;
  while(m)
    {
      if(i == x)
	i ++ ;
      for(int j = i + 1 ; j <= n && m; j ++)
	{
	  if(j != x)
	    {
	  cout << i << " " << j << endl ;
	  m -- ;
	    }
	}
      i ++ ;
    }
}


//iN tHe nAMe oF gOd
using namespace std ;
#include<bits/stdc++.h>
#include<cstdio>
#include<iostream>
#define int long long
#define ld long double
#define pld pair<ld , ld>
#define F first
#define S second
#define cerr cout << "*" << endl
#define pii pair <int , int>
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define vi vector <int>
#define si set<int>
#define msi multiset <int>
#define qi queue<int>
#define ashari cout << setprecision(8)
// #define cin fin
// #define cout fout
const int mod = 1e9 + 7 ;
const int inf = 1e18 ;
const int aval = 210 ;
int n , m , x ;

main()
{
  fast ;
  cin >> n >> m >> x ;
  if(m < n - 1 || m > (n * (n - 1)) / 2)
    {
      cout << -1 ;
      return 0 ;
    }
  for(int i = 1 ; i <= n ; i ++)
    if(i != x)
      cout << i << " "  << x << endl ;
  m -= n -1 ;
  int i = 1 ;
  while(m)
    {
      if(i == x)
	i ++ ;
      for(int j = i + 1 ; j <= n && m; j ++)
	{
	  if(j != x)
	    {
	  cout << i << " " << j << endl ;
	  m -- ;
	    }
	}
    }
}


//iN tHe nAMe oF gOd
using namespace std ;
#include<bits/stdc++.h>
#include<cstdio>
#include<iostream>
#define int long long
#define ld long double
#define pld pair<ld , ld>
#define F first
#define S second
#define cerr cout << "*" << endl
#define pii pair <int , int>
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define vi vector <int>
#define si set<int>
#define msi multiset <int>
#define qi queue<int>
#define ashari cout << setprecision(8)
// #define cin fin
// #define cout fout
const int mod = 1e9 + 7 ;
const int inf = 1e18 ;
const int aval = 210 ;
int n , m , x ;

main()
{
  fast ;
  cin >> n >> m >> x ;
  if(m < n - 1 || m > ((n * (n - 1)) / 2) - n + 1)
    {
      cout << -1 ;
      return 0 ;
    }
  for(int i = 1 ; i <= n ; i ++)
    if(i != x)
      cout << i << " "  << x << endl ;
  m -= n -1 ;
  int i = 1 ;
  int f = n ;
  if(f == x)
    f -- ;
  while(m)
    {
      if(i == x)
	i ++ ;
      for(int j = i + 1 ; j < f && m; j ++)
	{
	  if(j != x)
	    {
	  cout << i << " " << j << endl ;
	  m -- ;
	    }
	}
      i ++ ;
    }
}


//iN tHe nAMe oF gOd
using namespace std ;
#include<bits/stdc++.h>
#include<cstdio>
#include<iostream>
#define int long long
#define ld long double
#define pld pair<ld , ld>
#define F first
#define S second
#define cerr cout << "*" << endl
#define pii pair <int , int>
#define fast ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define vi vector <int>
#define si set<int>
#define msi multiset <int>
#define qi queue<int>
#define ashari cout << setprecision(8)
// #define cin fin
// #define cout fout
const int mod = 1e9 + 7 ;
const int inf = 1e18 ;
const int aval = 210 ;
int n , m , x ;

main()
{
  fast ;
  cin >> n >> m >> x ;
  if(m < n - 1 || m > ((n * (n - 1)) / 2) - n + 2)
    {
      cout << -1 ;
      return 0 ;
    }
  for(int i = 1 ; i <= n ; i ++)
    if(i != x)
      cout << i << " "  << x << endl ;
  m -= n -1 ;
  int i = 1 ;
  int f = n ;
  if(f == x)
    f -- ;
  while(m)
    {
      if(i == x)
	i ++ ;
      for(int j = i + 1 ; j < f && m; j ++)
	{
	  if(j != x)
	    {
	  cout << i << " " << j << endl ;
	  m -- ;
	    }
	}
      i ++ ;
    }
}


#include<bits/stdc++.h>
#include <iostream>

using namespace std;

int main()
{
    #ifdef LOCAL_EXEC
    #else
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #endif // LOCAL_EXEC
    long long int n,m,v,i,j,k,x,y,z;
    cin>>n>>m>>v;
    if(m<n-1||m>1+((n-1)*(n-2))/2){
        cout<<-1;
        return 0;
    }
    bool visited[n+1][n+1];
    for(i=1;i<=n;i++){
        for(j=1;j<=n;j++){
            visited[i][j]=false;
        }
    }
    vector<pair<long long int,long long int> >result;
    for(i=1;i<=n;i++){
        if(i==v)
            continue;
        result.push_back({v,i});
        visited[v][i]=true;
        visited[i][v]=true;
        m-=1;
        y=i;
    }
    x=1;
    while(m>0){
       for(i=1;i<=n;i++){
        if(i==x||i==y)
            continue;
        if(visited[x][i]==true)
                continue;
        result.push_back({x,i});
        visited[x][i]=true;
        visited[i][x]=true;
        m-=1;
        if(m==0)
            break;
       }
       x+=1;
    }
    for(i=0;i<result.size();i++){
        cout<<result[i].first<<" "<<result[i].second<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#include <iostream>

using namespace std;

int main()
{
    #ifdef LOCAL_EXEC
    #else
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #endif // LOCAL_EXEC
    long long int n,m,v,i,j,k,x,y,z;
    cin>>n>>m>>v;
    if(m<n-1||m>(n*(n-1))/2){
        cout<<-1;
        return 0;
    }
    bool visited[n+1][n+1];
    for(i=1;i<=n;i++){
        for(j=1;j<=n;j++){
            visited[i][j]=false;
        }
    }
    vector<pair<long long int,long long int> >result;
    for(i=1;i<=n;i++){
        if(i==v)
            continue;
        result.push_back({v,i});
        visited[v][i]=true;
        visited[i][v]=true;
        m-=1;
    }
    x=1;
    while(m>0){
       if(x==v)
       x+=1;
       for(i=1;i<=n;i++){
        if(i==x||visited[x][i]==true)
            continue;
        result.push_back({x,i});
        visited[x][i]=true;
        visited[i][x]=true;
        m-=1;
        if(m==0)
            break;
       }
       x+=1;
    }
    for(i=0;i<result.size();i++){
        cout<<result[i].first<<" "<<result[i].second<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#include <iostream>

using namespace std;

int main()
{
    #ifdef LOCAL_EXEC
    #else
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #endif // LOCAL_EXEC
    long long int n,m,v,i,j,k,x,y,z;
    cin>>n>>m>>v;
    k=((n-1)*(n-2))/2;
    if(m<n-1||m>1+k){
        cout<<-1;
        return 0;
    }
    bool visited[n+1]={false};
    vector<pair<int,int> >result;
    for(i=1;i<=n;i++){
        if(i==v)
            continue;
        result.push_back({v,i});
        m-=1;
        y=i;
    }
    visited[v]=true;
    x=1;
    while(m>0){
       if(visited[x]==true)
       x+=1;
       visited[x]=true;
       for(i=1;i<=n;i++){
        if(i==x||i==y||visited[i]==true)
            continue;
        result.push_back({x,i});
        m-=1;
        if(m==0)
            break;
       }
       x+=1;
    }
    for(i=0;i<result.size();i++){
        cout<<result[i].first<<" "<<result[i].second<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#include <iostream>

using namespace std;

int main()
{
    #ifdef LOCAL_EXEC
    #else
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #endif // LOCAL_EXEC
    long long int n,m,v,i,j,k,x,y,z;
    cin>>n>>m>>v;
    k=((n-1)*(n-2))/2;
    if(m<n-1||m>1+k){
        cout<<-1;
        return 0;
    }
    bool visited[n+1][n+1];
    for(i=1;i<=n;i++){
        for(j=1;j<=n;j++){
            visited[i][j]=false;
        }
    }
    vector<pair<long long int,long long int> >result;
    for(i=1;i<=n;i++){
        if(i==v)
            continue;
        result.push_back({v,i});
        visited[v][i]=true;
        visited[i][v]=true;
        m-=1;
        y=i;
    }
    x=1;
    while(m>0){
       for(i=1;i<=n;i++){
        if(i==x||i==y)
            continue;
        if(visited[x][i]==true)
                continue;
        result.push_back({x,i});
        visited[x][i]=true;
        visited[i][x]=true;
        m-=1;
        if(m==0)
            break;
       }
       x+=1;
    }
    for(i=0;i<result.size();i++){
        cout<<result[i].first<<" "<<result[i].second<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#include <iostream>

using namespace std;

int main()
{
    #ifdef LOCAL_EXEC
    #else
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #endif // LOCAL_EXEC
    long long int n,m,v,i,j,k,x,y,z;
    cin>>n>>m>>v;
    if(m<n-1||m>1+((n-1)*(n-2))/2){
        cout<<-1;
        return 0;
    }
    bool visited[n+1][n+1];
    for(i=1;i<=n;i++){
        for(j=1;j<=n;j++){
            visited[i][j]=false;
        }
    }
    vector<pair<long long int,long long int> >result;
    for(i=1;i<=n;i++){
        if(i==v)
            continue;
        result.push_back({v,i});
        visited[v][i]=true;
        visited[i][v]=true;
        m-=1;
        y=i;
    }
    x=1;
    while(m>0){
       if(x==v||x==y)
       x+=1;
       for(i=1;i<=n;i++){
        if(i==x||visited[x][i]==true||i==y)
            continue;
        result.push_back({x,i});
        visited[x][i]=true;
        visited[i][x]=true;
        m-=1;
        if(m==0)
            break;
       }
       x+=1;
    }
    for(i=0;i<result.size();i++){
        cout<<result[i].first<<" "<<result[i].second<<endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#include <iostream>

using namespace std;

int main()
{
    #ifdef LOCAL_EXEC
    #else
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    #endif // LOCAL_EXEC
    long long int n,m,v,i,j,k,x,y,z;
    cin>>n>>m>>v;
    if(m<n-1||m>n*(n-1)/2){
        cout<<-1;
        return 0;
    }
    bool visited[n+1][n+1];
    for(i=1;i<=n;i++){
        for(j=1;j<=n;j++){
            visited[i][j]=false;
        }
    }
    vector<pair<long long int,long long int> >result;
    for(i=1;i<=n;i++){
        if(i==v)
            continue;
        result.push_back({v,i});
        visited[v][i]=true;
        visited[i][v]=true;
        m-=1;
    }
    x=1;
    while(m>0){
       if(x==v)
       x+=1;
       for(i=1;i<=n;i++){
        if(i==x||visited[x][i]==true)
            continue;
        result.push_back({x,i});
        visited[x][i]=true;
        visited[i][x]=true;
        m-=1;
        if(m==0)
            break;
       }
       x+=1;
    }
    for(i=0;i<result.size();i++){
        cout<<result[i].first<<" "<<result[i].second<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n,m,v;
vector <int> num;
vector <pair <int,int> > ans;
int main()
{
    scanf("%d %d %d",& n,& m,& v);
    if(m<(n-1) || ((m-(n-1))>(n-3)*(n-2)/2)){
        printf("-1\n");
        return 0;
    }
    for(register int i=1;i<=n;i++){
        if(i==v) continue;
        else m--,printf("%d %d\n",i,v);
    }
    if(m==0) return 0;
    int u=v+1;
    if(v==n)
        u-=2;
    for(register int i=1;i<=n;i++)
    {

        for(register int j=i+1;j<=n;j++)
    	{
	        if(i!=v&&i!=u&&j!=v&&j!=u)
	        {
	            m--;
	            printf("%d %d\n",i,j);
	            if(m==0)
	                return 0;
	        }
    	}
 	}
    return 0;
}


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

#define inf 0x3f3f3f3f
typedef long long LL;
#define cls(x) memset(x,0,sizeof(x))
#define For(i,j,k) for(register int i=(j);i<=(k);++i)
#define Rep(i,j,k) for(register int i=(j);i>=(k);--i)
#define rint register int
#define il inline

const int N=1e5+7;
int n;
char s[N];

int main()
{
    scanf("%d%s",&n,s+1);
    int ans=0,cnt=0;
    for(rint i=1;i<=n;++i)
    {
        ans+=(s[i]!=s[i-1]);
        cnt+=(s[i]==s[i-1]);
    }
    if(cnt>2) cnt=2;
    printf("%d\n",ans+cnt);
    return 0;
}


#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;

int a[100005];

int main()
{
	int n,m,v;
	cin>>n>>m>>v;
	if(m<=n-1)
	{
		cout<<"-1"<<endl;
		return 0;
    }
    long long maxx=0;
    for(int i=1;i<=n-2;i++)
    maxx=maxx+i;
    maxx++;
    if(m>maxx)
    {
    	cout<<"-1"<<endl;
    	return 0;
	}
	int point=0;
	if(v==1)
	cout<<1<<" "<<n<<endl;
	else
	cout<<v-1<<" "<<v<<endl;
	m--;
	for(int i=v;i<=n;i++)
	{
		point++;
		a[point]=i;
	}
	for(int i=1;i<=v-2;i++)
	{
		point++;
		a[point]=i;
	}
	for(int i=1;i<=point-1;i++)
	{
		m--;
		if(a[i]<a[i+1])
		cout<<a[i]<<" "<<a[i+1]<<endl;
		else
		cout<<a[i+1]<<" "<<a[i]<<endl;
	}
	for(int i=1;i<=point-2;i++)
	for(int j=i+2;j<=point;j++)
	{
		if(m==0)
		break;
		if(a[i]<a[j])
		cout<<a[i]<<" "<<a[j]<<endl;
		else
		cout<<a[j]<<" "<<a[i]<<endl;
		m--;
	}
	return 0;
}


#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;

int a[100005];

int main()
{
	int n,m,v;
	scanf("%d%d%d",&n,&m,&v);
	if(m<n-1)
	{
		cout<<"-1"<<endl;
		return 0;
    }

    long long maxx=0;
	for(int i=1;i<=n-3;i++)
    maxx=maxx+i;
    maxx+=n-1;
    if(m>maxx)
    {
    	cout<<"-1"<<endl;
    	return 0;
	}

	int point=0;
	if(v==1)
	cout<<1<<" "<<n<<endl;
	else
	cout<<v-1<<" "<<v<<endl;
	m--;

	if(v!=1)
	{
		for(int i=1;i<=v-2;i++)
     	{
	    	point++;
     		a[point]=i;
     	}
		for(int i=v;i<=n;i++)
    	{
    		point++;
     		a[point]=i;
      	}
	}
	else
	{
		for(int i=1;i<=n-1;i++)
		{
			point++;
			a[point]=i;
		}
	}

	for(int i=1;i<=point-1;i++)
	{
		m--;
		cout<<a[i]<<" "<<a[i+1]<<endl;
	}

	for(int i=1;i<=point-2;i++)
	for(int j=i+2;j<=point;j++)
	{
		if(m==0)
		break;
		cout<<a[i]<<" "<<a[j]<<endl;
		m--;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int s[100005];
int main()
{
    int n, m, v;
    cin >> n >> m >> v;
    for (int i = 1; i <= n; i++)
        s[i] = i;
    swap(s[1], s[v]);
    int tmp = 0;
    if (n > 2) tmp = ( 1 + (n - 3) ) * (n - 3) / 2;
    if (m < n - 1 || (m > n - 1 + tmp))
        cout << -1 << endl;
    else
    {
        for (int i = 2; i <= min(n, m); i++)
            cout << s[1] << ' ' << s[i] << endl;
        int num = m - (n - 1);
        for (int i = 3; i <= n - 1 && num > 0; i++)
            for (int j = 2; j < i && num > 0; j++)
            {
                cout << s[j] << ' ' << s[i] << endl;
                num--;
            }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
inline void out(int X){
	if(X<0) {X=~(X-1); putchar('-');}
	if(X>9) out(X/10);
	putchar(X%10+'0');
}
vector <int> a;
int main(){
	int n,m,k;
	cin>>n>>m>>k;
	if(m<n-1||m>n-1+(n-3)*(n-2)/2){
		out(-1);return 0;
	}
	for(int i=1;i<=n;i++){
		if(i!=k){
			a.push_back(i);
			out(i);putchar(' ');out(k);
			putchar('\n');
		}
	}
	m-=n-1;
	for(int i=0;i<a.size()-2;i++){
		for(int j=i+1;j<a.size()-1;j++){
			if(m==0)return 0;
			m--;
			out(a[i]);putchar(' ');out(a[j]);
			putchar('\n');
		}
	}
	return 0;
}