Language: C++

#include <iostream>
#include <string>
#include <algorithm>
#include <cassert>
#include <set>
#include <vector>
#include <cmath>
#include <bitset>
#include <functional>
#include <limits>
#include <map>
using namespace std;

typedef long long ll;
#define rep1(i, n) for(ll i = 1; i <=(ll)(n); i++)
#define rep0(i, n) for(ll i = 0; i <=(ll)(n); i++)
#define mul_mod(multiplicand, multiplier, mod) ((multiplicand*(multiplier % mod)) % mod)

void mul1(ll *product, ll *idx, ll a, ll *taken){
    ll mod = 1e9 + 7;
    *product = mul_mod(*product, a, mod);
    *idx -= 1;
    *taken += 1;
}

void mul2(ll *product,ll *idx,ll a,ll b,ll *taken){
    ll mod = 1e9 + 7;
    *product = mul_mod(*product, a*b, mod);
    *idx -= 2;
    *taken += 2;
}

int main(){
    ll n,k;
    ll mod = 1e9 + 7;
    cin >> n >> k;
    vector<ll> pos;
    vector<ll> neg;
    ll cnt_zero = 0;
    ll maxa = -mod;
    rep1(i,n){
        ll tmp;
        cin >> tmp;
        maxa = max(maxa,tmp);
        if(tmp == 0){
            cnt_zero++;
            continue;
        }
        if(tmp > 0) pos.push_back(tmp);
        else neg.push_back(abs(tmp));
    }
    int pos_len = pos.size();
    int neg_len = neg.size();
    sort(pos.begin(), pos.end());
    sort(neg.begin(), neg.end());
    ll product = 1;

    //obvious case
    if(k == 1){
        cout << maxa % mod << endl;
        return 0;
    }
    if(cnt_zero == n){
        cout << 0 << endl;
        return 0;
    }
    if(n == k){
        if(cnt_zero > 0){
            product = 0;
        }else{
            rep0(i, pos_len-1) product = mul_mod(product,pos[i],mod);
            rep0(i, neg_len-1) product = mul_mod(product,neg[i],mod);    
        }
        cout << product << endl;
        return 0;
    }else{
        if(neg_len == 0){ //only 0 or positive
            rep1(i,cnt_zero) pos.insert(pos.begin(),0);
            pos_len += cnt_zero;
            for(int i = pos_len - 1;i >= pos_len - k;i--){
                product = mul_mod(product,pos[i],mod);
            }
            cout << product << endl;
            return 0;
        }else if(pos_len == 0){  //only 0 or negative
            if(cnt_zero == 0){
                if(k % 2 == 0){
                    for(int i = neg_len - 1;i >= neg_len - k;i--){
                        product = mul_mod(product,neg[i],mod);
                    }
                }else{
                    rep0(i,k-1) product = mul_mod(product,neg[i],mod);
                    product = (-product + mod) % mod;
                }
            }else{
                rep1(i,cnt_zero) neg.insert(neg.begin(),0);
                neg_len += cnt_zero;
                if(k % 2 == 0){
                    for(int i = neg_len - 1;i >= neg_len - k;i--){
                        product = mul_mod(product,neg[i],mod);
                    }
                }else{
                    product = 0;
                }
            }
            cout << product << endl;
            return 0;
        }
    }
    if( pos_len + neg_len < k){
        cout << 0 << endl;
        return 0;
    }

    //not obvious case
    rep1(i,cnt_zero) pos.insert(pos.begin(),0);
    pos_len += cnt_zero;
    ll pos_idx = pos_len - 1;
    ll neg_idx = neg_len - 1;
    ll taken = 0;
    while(taken < k){
        if(taken == 0 && (k % 2 == 1)){
            mul1(&product, &pos_idx, pos[pos_idx], &taken);
            continue;
        }
        if(pos_idx < 0){
            mul2(&product, &neg_idx, neg[neg_idx],neg[neg_idx-1], &taken);
        }else if(neg_idx <= 0){
            mul1(&product, &pos_idx, pos[pos_idx], &taken);
        }else{
            if(neg_idx == 0){
                mul1(&product, &pos_idx, pos[pos_idx], &taken);
            }else if(pos_idx == 0){
                //if(k - taken == 1) mul1(&product, &pos_idx, pos[pos_idx], &taken);
                else mul2(&product, &neg_idx, neg[neg_idx],neg[neg_idx-1], &taken);
            }else{
                ll pos_product = pos[pos_idx]*pos[pos_idx-1];
                ll neg_product = neg[neg_idx]*neg[neg_idx-1];
                if(pos_product >= neg_product){
                    mul2(&product, &pos_idx, pos[pos_idx],pos[pos_idx-1], &taken);
                }else{
                    mul2(&product, &neg_idx, neg[neg_idx],neg[neg_idx-1], &taken);
                }
            }
        }    
    }
cout << product << endl;
return 0;
}

/*
5 2
-1 -2 -5 -1 -3
5 3
7 -1 8 -3 -4
*/

Compiler output:
a.cc: In function 'int main()':
a.cc:135:17: error: expected '}' before 'else'
  135 |                 else mul2(&product, &neg_idx, neg[neg_idx],neg[neg_idx-1], &taken);
      |                 ^~~~
a.cc:133:35: note: to match this '{'
  133 |             }else if(pos_idx == 0){
      |                                   ^
a.cc:136:14: error: 'else' without a previous 'if'
  136 |             }else{
      |              ^~~~
a.cc: At global scope:
a.cc:147:1: error: 'cout' does not name a type
  147 | cout << product << endl;
      | ^~~~
a.cc:148:1: error: expected unqualified-id before 'return'
  148 | return 0;
      | ^~~~~~
a.cc:149:1: error: expected declaration before '}' token
  149 | }
      | ^