Problem:
Score : 500 points

Problem Statement

Given are N integers A_1,\ldots,A_N.

We will choose exactly K of these elements. Find the maximum possible product of the chosen elements.

Then, print the maximum product modulo (10^9+7), using an integer between 0 and 10^9+6 (inclusive).

Constraints

1 \leq K \leq N \leq 2\times 10^5

|A_i| \leq 10^9

Input

Input is given from Standard Input in the following format:

N K
A_1 \ldots A_N

Output

Print the maximum product modulo (10^9+7), using an integer between 0 and 10^9+6 (inclusive).

Sample Input 1

4 2
1 2 -3 -4

Sample Output 1

12

The possible products of the two chosen elements are 2, -3, -4, -6, -8, and 12, so the maximum product is 12.

Sample Input 2

4 3
-1 -2 -3 -4

Sample Output 2

1000000001

The possible products of the three chosen elements are -24, -12, -8, and -6, so the maximum product is -6.

We print this value modulo (10^9+7), that is, 1000000001.

Sample Input 3

2 1
-1 1000000000

Sample Output 3

1000000000

The possible products of the one chosen element are -1 and 1000000000, so the maximum product is 1000000000.

Sample Input 4

10 10
1000000000 100000000 10000000 1000000 100000 10000 1000 100 10 1

Sample Output 4

999983200

Be sure to print the product modulo (10^9+7).

Solution in C++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#pragma GCC optimize ("-O3")
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
//#include <boost/multiprecision/cpp_int.hpp>
//using namespace boost::multiprecision;
using namespace std;

#define all(c) (c).begin(),(c).end()
#define endl "\n"
#define ff first
#define ss second
#define allr(c) (c).rbegin(),(c).rend()
#define ifr(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define pof pop_front
#define pob pop_back
#define pb emplace_back
#define pf emplace_front
#define fstm(m,n,r) m.reserve(n);m.max_load_factor(r)
#define mp make_pair
#define mt make_tuple
#define inf LLONG_MAX
#define os tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
//order_of_key (k) : Number of items strictly smaller than k .
//find_by_order(k) : K-th element in a set (counting from zero).
const double PI = acos(-1);
typedef complex<double> cd;
typedef long long ll;
ll gcd(){return 0ll;} template<typename T,typename... Args> T gcd(T a,Args... args) { return __gcd(a,(__typeof(a))gcd(args...)); }
typedef map<ll,ll> mll;
typedef map<string,ll> msll;
typedef unordered_map<ll,ll> umap;
typedef vector<ll> vll;
typedef pair<ll,ll> pll;
typedef long double ld;
#define mod 1000000007 
#define N 100001

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
    #endif
    // int T;cin>>T;while(T--) 
    {
        ll n, k, t;
        cin>>n>>k;
        vll pos, neg; ll zero = 0;
        ifr(i, 0, n)  {
            cin>>t;
            if(t==0) zero++;
            else if(t<0) neg.pb(t);
            else pos.pb(t);
        }
        ll ps = pos.size(), ns = neg.size();
        if(k > (ps + ns)) {
            cout<<"0\n";
            return 0;
        }

        ll ans = 1;
        if( k == (ps + ns)) {
            if((ns&1) && zero>0) {
                cout<<"0\n";
                return 0;
            }
            ifr(i, 0, ps) ans = (ans*pos[i])%mod;
            ifr(i, 0, ns) ans = (ans*neg[i])%mod;
            ans = (ans + mod)%mod;
            cout<<ans<<endl;
            return 0;
        }

        sort(all(pos), greater<ll>());
        sort(all(neg));

        if(ns == 0) {
            ans = 1;
            ifr(i, 0, k) ans = (ans*pos[i])%mod;
            cout<<ans<<endl;
            return 0;
        }


        if(ps == 0) {
            if(k&1) reverse(all(neg));
            if(zero>0) {
                cout<<"0\n";
                return 0;
            }
            ans = 1;
            ifr(i, 0, k) ans = (ans*neg[i])%mod;
            ans = (ans + mod)%mod;
            cout<<ans<<endl;
            return 0;
        }

        vector<ld> pre;

        pre.pb(0);
        ifr(i, 0, ps) pre.pb(pre.back() + log(pos[i]));

        ld lg = 0, sum, msum = 0; 
        int x=-1, y=-1;

        for(int i=0; i<=ns; i++) {

            if(k-i<0 ) break;

            if(((k-i)>ps) || (i&1)) {
                lg += log(-neg[i]);
                continue;
            }

            sum = lg + pre[k-i];

            // cout<<sum<<" "<<msum<<endl;

            if(msum < sum) {
                msum = sum;
                x = i; y = k-i;
            }

            if(i!=ns) lg += log(-neg[i]);
        }

        // cout<<x<<" "<<y<<endl;
        if(x==-1) {
            cout<<0<<endl;
            return 0;
        }

        ans = 1;
        if((x&1) && zero>0 ) {
            cout<<"0\n";
            return 0;
        }
        ifr(i, 0, x) ans = (1ll*ans*neg[i])%mod;
        ifr(i, 0, y) ans = (1ll*ans*pos[i])%mod;
        ans = (ans + mod)%mod;
        cout<<ans<<endl;
    }
    return 0;
}