Language: C++

#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for(ll i = 0; i < (ll)(n); i++)
#define repd(i,n) for(ll i = n - 1; i >= 0; i--)
#define all(x) x.begin(), x.end()

int main() {
cin.tie(0);
ios::sync_with_stdio(false);

int n, k;
cin >> n >> k;
vevtor<int> a(n);
rep(i, n) {cin >> a[i];}
    
sort(all(a), [](int i, int j) -> bool {return abs(i) > abs(j);});

bool z = false;
if(a[n - 1] == 0) {
    z = true;
    for(auto it = a.end() - 2; it >= a.begin(); --it) {
        if(*it != 0) {a.erase(it + 1, a.end()); break;}
    }
    if((int)a.size() == n) {a.clear();}
    n = (int)a.size();
}

ll prd = 1;
int m = pow(10, 9) + 7;
if(n < k) {
    cout << 0 << endl;
} else if(n == k) {
    rep(i, k) {
        prd *= a[i];
        prd %= m;
    }
    
    if(prd > 0) {
        cout << prd << endl;
    } else {
        if(z) {
            cout << 0 << endl;
        } else {
            prd += m;
            cout << prd << endl;
        }
    }
} else {
    rep(i, k) {
        prd *= a[i];
        prd %= m;
    }
    
    if(prd > 0) {
        cout << prd << endl;
    } else {
        vector<int> f(2, n);
        vector<int> b(2, n);
        
        if(a[k - 1] > 0) {
            f[1] = k - 1;
            repd(i, k - 1) {
                if(a[i] < 0) {f[0] = i; break;}
            }
        } else {
            f[0] = k - 1;
            repd(i, k - 1) {
                if(a[i] > 0) {f[1] = i; break;}
            }
        }
        if(a[k] > 0) {
            b[1] = k;
            for(int i = k + 1; i < n; i++) {
                if(a[i] < 0) {b[0] = i; break;}
            }
        } else {
            b[0] = k;
            for(int i = k + 1; i < n; i++) {
                if(a[i] > 0) {b[1] = i; break;}
            }
        }
        
        int fi = 2;
        if(b[1] != n && f[0] != n) {
            if(b[0] == n || f[1] == n) {
                fi = 0;
            } else {
                if(a[f[1]] * a[b[1]]>a[f[0]] * a[b[0]]) {
                    fi = 0;
                } else {
                    fi = 1;
                }
            }
        } else {
            if(b[1] == n || f[0] == n) {
                fi = 1;
            }
        }
        
        if(fi == 2) {
            if(z) {
                cout << 0 << endl;
            } else {
                prd = 1;
                for(int i = n - 1; i >= n - k; i--) {
                    prd *= a[i];
                    prd %= m;
                }
                prd += m;
                cout << prd << endl;
            }
        } else {
            int y, v, s, t, k;
            s = m; t = a[f[fi]];
            y = 0; v = 1;
            while(t) {
                k = s / t;
                s -= k * t;
                swap(s, t);
                y -= k * v;
                swap(y, v);
            }
            y %= m;
            
            prd *= y;
            prd %= m;
            prd *= a[b[1 - fi]];
            prd %= m;
            if(prd < 0) {prd += m;}
            cout << prd << endl;
        }
    }
}
}


Compiler output:
a.cc: In function 'int main()':
a.cc:15:1: error: 'vevtor' was not declared in this scope
   15 | vevtor<int> a(n);
      | ^~~~~~
a.cc:15:8: error: expected primary-expression before 'int'
   15 | vevtor<int> a(n);
      |        ^~~
a.cc:16:19: error: 'a' was not declared in this scope
   16 | rep(i, n) {cin >> a[i];}
      |                   ^
a.cc:18:10: error: 'a' was not declared in this scope
   18 | sort(all(a), [](int i, int j) -> bool {return abs(i) > abs(j);});
      |          ^
a.cc:7:16: note: in definition of macro 'all'
    7 | #define all(x) x.begin(), x.end()
      |                ^