Language: C++

#include <bits/stdc++.h>

using std::cin;
using std::cout;
using std::endl;
using std::string;
using std::vector;
using std::bitset;

#define rep(i,n)    for(int i=0; i<(int)n; i++)
#define reps(i,s,e) for(int i=s; i<(int)e; i++)
#define coutd(i) cout << std::fixed << std::setprecision(i)
typedef long double ld;
typedef long long ll;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef std::pair<int, int> pii;
typedef std::pair<ll, ll> pll;

template<class T>
using Set = std::set<T>;
template<class T, class U>
using Map = std::map<T, U>;

template<class T>
using Graph = vector<vector<T>>;

const int INT_BIG = 1<<28;


// #include <iostream>

// modint: mod 計算を int を扱うように扱える構造体
template<int MOD> struct Fp {
    long long val;
    constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
        if (val < 0) val += MOD;
    }
    constexpr int getmod() { return MOD; }
    constexpr operator int() const noexcept {
        return (int)val;
    }
    constexpr Fp operator - () const noexcept {
        return val ? MOD - val : 0;
    }
    constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }
    constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }
    constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }
    constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }
    constexpr Fp& operator += (const Fp& r) noexcept {
        val += r.val;
        if (val >= MOD) val -= MOD;
        return *this;
    }
    constexpr Fp& operator -= (const Fp& r) noexcept {
        val -= r.val;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr Fp& operator *= (const Fp& r) noexcept {
        val = val * r.val % MOD;
        return *this;
    }
    constexpr Fp& operator /= (const Fp& r) noexcept {
        long long a = r.val, b = MOD, u = 1, v = 0;
        while (b) {
            long long t = a / b;
            a -= t * b; std::swap(a, b);
            u -= t * v; std::swap(u, v);
        }
        val = val * u % MOD;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr bool operator == (const Fp& r) const noexcept {
        return this->val == r.val;
    }
    constexpr bool operator != (const Fp& r) const noexcept {
        return this->val != r.val;
    }
    friend constexpr std::ostream& operator << (std::ostream &os, const Fp<MOD>& x) noexcept {
        return os << x.val;
    }
    friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
        if (n == 0) return 1;
        auto t = modpow(a, n / 2);
        t = t * t;
        if (n & 1) t = t * a;
        return t;
    }
};

const int MOD = 1000000007;

int main() {
    int N, K;
    cin >> N >> K;
    vll positive;
    vll negative;
    int positiveCount = 0;
    int negativeCount = 0;
    rep(i, N) {
        ll a;
        cin >> a;
        if (a > 0) {
            positive.push_back(a);
            positiveCount++;
        } else if (a < 0) {
            negative.push_back(a);
            negativeCount++;
        }
    }

    bool negativeAns = false;
    if (positiveCount == 0 && K % 2 != 0) {
        negativeAns = true;
    } else if (N == K && negativeAns % 2 == 1) {
        negativeAns = true;
    }
    std::sort(positive.begin(), positive.end());
    std::sort(negative.begin(), negative.end());

    if (negativeAns) {
        int neglen = negative.size();
        Fp<MOD> ans(1);
        rep(i, K) {
            ans *= Fp<MOD>(negative[neglen - i - 1]);
            // cout << ans << endl;
        }
        cout << ans << endl;
    } else {
        vector<Fp<MOD>> negneg;
        int neglen = negative.size();
        for (int i = 0; i <= neglen - 2; i += 2) {
            negneg.push_back(negative[i] * negative[i+1]);
        }
        Fp<MOD> ans(1);
        int posindex = positive.size() -1;
        int negindex = 0;
        int negsize = negneg.size();
        int j = 0;
        while(j < K) {
            if (negindex == negsize) {
                if (posindex == -1) {
                    cout << 0 << endl;
                    return;
                }
                ans *= Fp<MOD>(positive[posindex]);
                posindex--;
                j++;
            } else if (posindex == -1) {
                ans *= Fp<MOD>(negneg[negindex]);
                negindex++;
                j += 2;
            } else if (positive[posindex] > negneg[negindex] || (K - j == 1)) {
                ans *= Fp<MOD>(positive[posindex]);
                posindex--;
                j++;
            } else if (posindex == 0) {
                ans *= Fp<MOD>(negneg[negindex]);
                negindex++;
                j += 2;
            } else if (negneg[negindex] > positive[posindex] * positive[posindex-1]) {
                ans *= Fp<MOD>(negneg[negindex]);
                negindex++;
                j += 2;
            } else {
                ans *= Fp<MOD>(positive[posindex] * positive[posindex-1]);
                posindex -= 2;
                j += 2;
            }
        }
        cout << ans << endl;
    }
}

Compiler output:
a.cc: In function 'int main()':
a.cc:146:21: error: return-statement with no value, in function returning 'int' [-fpermissive]
  146 |                     return;
      |                     ^~~~~~