Language: C++

#include<bits/stdc++.h>
#define pb emplace_back
using namespace std;
using ll = long long;
const int maxn = 300010, p = 1e9+7;
ll bin_pow(ll v, ll t) {
	ll res = 1;
	for (;t;t>>=1, v = v * v % p)
		if (t&1)
			res = res * v % p;
	return res;
}
signed main(){
	ios_base::sync_with_stdio(0), cin.tie(0);
	int n, k;
	cin >> n >> k;
	vector<ll> a(n);
	for (auto &i : a)
		cin >> i;

	priority_queue<ll> pos, neg;
	int c0 = 0;
	for (auto i : a) {
		if (i == 0) {
			++c0;
			continue;
		}
		(i>0?pos:neg).push(abs(i));
	}
	//cerr << c0 << '\n';

	if (pos.size() + neg.size() < k || (pos.size()+neg.size()==k && (neg.size()&1)) {
		return cout << 0 << '\n', 0;
	}

	if (pos.empty() && (k&1)) {
		if (c0)
			return cout << 0 << '\n', 0;
		sort(a.begin(), a.end(), greater<>());
		ll res = 1;
		for (int i = 0;i < k;++i)
			res = res * a[i] % p;
		if (res < 0) res += p;
		cout << res << '\n';
		return 0;
	}
//	for (int i = 0;i < n;++i)
//		pos.pb(0), neg.pb(0);

	ll res = 1, si = 1, cnt = 0;
	int i = 0, j = 0;
	int lstp = -1, lstn = -1;
	while (cnt < k) {
		if (pos.empty() || (neg.size() && neg.top() >= pos.top())) {
			res = res * neg.top() % p;
			lstn = neg.top();
			neg.pop();
			si *= -1;
		}
		else {
			res = res * pos.top() % p;
			lstp = pos.top();
			pos.pop();
		}
		++cnt;
	}
	if (res < 0) res += p;
	if (si == -1) {
		ll a, b, c, d;
		a = lstp, b = lstn, c = (pos.size()?pos.top():-1), d = (neg.size()?neg.top():-1);
		cerr << "A B C D " << a << ' ' << b << ' ' << c << ' ' << d << '\n';
		if ((a==-1||d==-1) && (c==-1||b==-1))
			res = 0;
		if (a != -1 && d != -1 && (b==-1||c==-1||(d*b>=a*c))) 
			res = res * bin_pow(a, p-2) % p * d % p;
		else 
			res = res * bin_pow(b, p-2) % p * c % p;

	}
	if (res < 0) res += p;
		cout << res << '\n';
}


Compiler output:
a.cc: In function 'int main()':
a.cc:32:88: error: expected ';' before '{' token
   32 |         if (pos.size() + neg.size() < k || (pos.size()+neg.size()==k && (neg.size()&1)) {
      |                                                                                        ^~
      |                                                                                        ;
a.cc:36:9: error: expected primary-expression before 'if'
   36 |         if (pos.empty() && (k&1)) {
      |         ^~
a.cc:34:10: error: expected ')' before 'if'
   34 |         }
      |          ^
      |          )
   35 | 
   36 |         if (pos.empty() && (k&1)) {
      |         ~~
a.cc:32:12: note: to match this '('
   32 |         if (pos.size() + neg.size() < k || (pos.size()+neg.size()==k && (neg.size()&1)) {
      |            ^
a.cc:53:16: error: 'cnt' was not declared in this scope; did you mean 'int'?
   53 |         while (cnt < k) {
      |                ^~~
      |                int
a.cc:55:25: error: 'res' was not declared in this scope
   55 |                         res = res * neg.top() % p;
      |                         ^~~
a.cc:58:25: error: 'si' was not declared in this scope; did you mean 'i'?
   58 |                         si *= -1;
      |                         ^~
      |                         i
a.cc:61:25: error: 'res' was not declared in this scope
   61 |                         res = res * pos.top() % p;
      |                         ^~~
a.cc:67:13: error: 'res' was not declared in this scope
   67 |         if (res < 0) res += p;
      |             ^~~
a.cc:68:13: error: 'si' was not declared in this scope; did you mean 'i'?
   68 |         if (si == -1) {
      |             ^~
      |             i
a.cc:73:25: error: 'res' was not declared in this scope
   73 |                         res = 0;
      |                         ^~~
a.cc:75:25: error: 'res' was not declared in this scope
   75 |                         res = res * bin_pow(a, p-2) % p * d % p;
      |                         ^~~
a.cc:77:25: error: 'res' was not declared in this scope
   77 |                         res = res * bin_pow(b, p-2) % p * c % p;
      |                         ^~~
a.cc:80:13: error: 'res' was not declared in this scope
   80 |         if (res < 0) res += p;
      |             ^~~
a.cc:81:25: error: 'res' was not declared in this scope
   81 |                 cout << res << '\n';
      |                         ^~~