Language: C++

#define LOCAL
#include <bits/stdc++.h>
#define rep(i, n) for(int i=0; i<(n); ++i)
#define rrep(i, n) for(int i=(n-1); i>=0; --i)
#define rep2(i, s, n) for(int i=s; i<(n); ++i)
#define ALL(v) (v).begin(), (v).end()
#define memr(dp, val) memset(dp, val, sizeof(dp))
using namespace std;
typedef long long ll;
static const int INTINF = (INT_MAX >> 1); // 10^9 + 10^7
static const ll LLINF = (LLONG_MAX >> 1);
static const int MAX = 1e5+1;
static const ll MOD = 1e9+7;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T> inline bool int_ceil(T a, T b) { T res = a / b; if(a % b != 0) res++; return res; }
template<typename T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T> >;
string to_string(string s) { return '"' + s + '"'; }
string to_string(bool b) { return b ? "true" : "false"; }
template <size_t N> string to_string(bitset<N> bs) {
  string res;
  for (size_t i = 0; i < N; ++i) res += '0' + bs[i];
  return res;
}
string to_string(vector<bool> v) {
  string res = "{";
  for (bool e : v) res += to_string(e) + ", ";
  return res += "}";
}
template <class T, class U> string to_string(pair<T, U> p);
template <class C> string to_string(C c) {
  string res = "{";
  for (auto e : c) res += to_string(e) + ", ";
  return res += "}";
}
template <class T, class U> string to_string(pair<T, U> p) {
  return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}
void debug() { cerr << '\n'; }
template <class Head, class... Tail> void debug(Head head, Tail... tail) {
  cerr << '\t' << to_string(head), debug(tail...);
}
#ifdef LOCAL
#define print(...) cerr << "[" << #__VA_ARGS__ << "]:", debug(__VA_ARGS__)
#else
#define print(...)
#endif


// modint: mod 計算を int を扱うように扱える構造体
template<int MOD> struct Fp {
    long long val;
    constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
        if (val < 0) v += MOD;
    }
    constexpr int getmod() { return MOD; }
    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; swap(a, b);
            u -= t * v; 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 ostream& operator << (ostream &os, const Fp<MOD>& x) noexcept {
        return os << x.val;
    }
    friend constexpr istream& operator >> (istream &is, Fp<MOD>& x) noexcept {
        return is >> 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;
    }
};


using mint = Fp<MOD>;


int main(int argc, const char * argv[]) {
	std::cout << std::fixed << std::setprecision(15);

	ll N, K; cin >> N >> K;
	vector<pair<ll, ll> > v(N);
	bool zerohukumi = false;
	rep(i, N){
		ll a; cin >> a;
		if(a == 0) {
			zerohukumi = true;
			continue;
		}
		pair<ll, ll> p = {abs(a), 1};
		if(a < 0) p.second = -1;
		v[i] = p;
	}

	if(v.size() < K){
		cout << 0 << endl;
		return 0;
	}

	sort(ALL(v));
	ll sum = 1;
	mint ans = 1;
	int cnt_i = 0;
	ll rmax = -LLINF;
	ll rmin = LLINF;
	ll lmax = -LLINF;
	ll lmin = LLINF;
	rrep(i, v.size()){
		ll pnum = v[i].first * v[i].second;
		if(cnt_i < K){
			chmax(rmax, pnum); chmin(rmin, pnum);
			sum = sum * pnum % MOD;
			ans *= pnum;
			cnt_i++;
		}
		else{
			chmax(lmax, pnum); chmin(lmin, pnum);
		}
	}


	if(sum < 0){
		if(N == K){
			if(ans.val < 0) {
				ans += MOD - 1;
				ans += 1;
			}
			cout << ans << endl;
			return 0;
		}
		bool ok =false;
		mint ans3 = ans;
		mint ans4 = ans;
		if(rmin < 0 && lmax >= 0){
			ans3 /= rmin; ans3 *= lmax;
			ok = true;
		}
		if(rmax >= 0 && lmin < 0){
			ans4 /= rmax; ans4 *= lmin;
			ok = true;
		}
		if(!ok){
			if(zerohukumi){
				cout << 0 << endl;
				return 0;
			}
			mint ans2 = 1;
			rep(i, K){
				mint pnum = v[i].first * v[i].second;
				ans2 = ans2 * pnum;
			}
			if(ans2.val < 0) {
				ans2 += MOD - 1;
				ans2 += 1;
			}
			cout << ans2 << endl;
		}
		ans = max(ans3.val, ans4.val);
		else{
			if(ans.val < 0) {
				ans += MOD - 1;
				ans += 1;
			}
			cout << ans << endl;
			return 0;
		}
	}
	else{
		if(ans.val < 0) {
			ans += MOD - 1;
			ans += 1;
		}
		cout << ans << endl;
		return 0;
	}

	return 0;
}


Compiler output:
a.cc: In function 'int main(int, const char**)':
a.cc:196:17: error: expected '}' before 'else'
  196 |                 else{
      |                 ^~~~
a.cc:159:20: note: to match this '{'
  159 |         if(sum < 0){
      |                    ^
a.cc: At global scope:
a.cc:205:9: error: expected unqualified-id before 'else'
  205 |         else{
      |         ^~~~
a.cc:214:9: error: expected unqualified-id before 'return'
  214 |         return 0;
      |         ^~~~~~
a.cc:215:1: error: expected declaration before '}' token
  215 | }
      | ^