Language: C++

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vl = vector<ll>;
using pll = pair<ll, ll>;

#define rep(i, n) for (ll i = 0, xxx_rep_end = (n); i < xxx_rep_end; ++i)
#define all(x) (x).begin(), (x).end()

template <class T1, class T2>
inline void chmax(T1 &a, const T2 &b)
{
    if (a < b)
    {
        a = b;
    }
}
template <class T1, class T2>
inline void chmin(T1 &a, const T2 &b)
{
    if (a > b)
    {
        a = b;
    }
}

constexpr ll mod = 1000000007;

int main()
{
    ll N, K;
    cin >> N >> K;
    vector<pair<uint64_t, bool>> V(N);
    ll cnt_p = 0;
    ll cnt_m = 0;

    rep(i, N)
    {
        ll num;
        cin >> num;
        if (num > 0)
        {
            V[i] = make_pair(num, true);
            ++cnt_p;
        }
        else if (num <= 0)
        {
            V[i] = make_pair(abs(num), false);
            ++cnt_m;
        }
    }

    ll ans = 1;
    if ((N == K && cnt_m % 2 == 1) || (cnt_p == 0 && K % 2 == 1))
    {
        sort(all(V));
        rep(i, K)
        {
            ans *= V[i].first % mod;
            ans %= mod;
        }
        if (ans == 0)
        {
            ans = 0;
        }
        else
        {
            ans = -ans + mod;
        }
    }
    else
    {
        sort(all(V), greater<pair<uint64_t, bool>>());
        ll fugou = 1;
        ll last_p = -1;
        ll last_m = -1;
        rep(i, K)
        {
            if (V[i].second)
            {
                last_p = i;
            }
            else
            {
                last_m = i;
                fugou *= -1;
            }
        }

        if (fugou == 1)
        {
            rep(i, K)
            {
                ans *= V[i].first;
                ans %= mod;
            }
        }
        else
        {
            ll next_p = -1;
            ll next_m = -1;
            for (ll i = K; i < N; ++i)
            {
                if (V[i].second)
                {
                    next_p = i;
                }
                else
                {
                    next_m = i;
                }

                if (next_p != -1 && next_m != -1)
                {
                    break;
                }
            }

            ll id_ex = -1;
            ll id_in = -1;
            if (last_p != -1 && last_m != -1 && next_p != -1 && next_m != -1)
            {
                if (V[last_m].first * V[next_m].first >= V[last_p].first * V[next_p].first)
                {
                    id_ex = last_p;
                    id_in = next_m;
                }
                else
                {
                    id_ex = last_m;
                    id_in = next_p;
                }
            }
            else if (last_p == -1 || next_m == -1)
            {
                id_ex = last_m;
                id_in = next_p;
            }
            else if (last_m == -1 || next_p == -1)
            {
                id_ex = last_p;
                id_in = next_m;
            }

            rep(i, K)
            {
                if (i == id_ex)
                {
                    continue;
                }
                ans *= V[i].first % mod;
                ans %= mod;
            }
            ans *= V[id_in].first;
            ans %= mod;
        }
    }
    cout << ans << endl;


Compiler output:
a.cc: In function 'int main()':
a.cc:159:25: error: expected '}' at end of input
  159 |     cout << ans << endl;
      |                         ^
a.cc:31:1: note: to match this '{'
   31 | {
      | ^