Language: C++

#include<bits/stdc++.h>
#define rep(i,n) for (int i=0;i<(n);i++)
using namespace std;
using P = pair<int,int>;
using ll = long long;

const int mod = 1000000007;
#define MOD 1000000007
struct mint {
  ll x; // typedef long long ll;
  mint(ll x=0):x((x%mod+mod)%mod){}
  mint operator-() const { return mint(-x);}
  mint& operator+=(const mint a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
  mint operator+(const mint a) const { return mint(*this) += a;}
  mint operator-(const mint a) const { return mint(*this) -= a;}
  mint operator*(const mint a) const { return mint(*this) *= a;}
  mint pow(ll t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }
 
  // for prime mod
  mint inv() const { return pow(mod-2);}
  mint& operator/=(const mint a) { return *this *= a.inv();}
  mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, mint& a) { return is >> a.x;}
ostream& operator<<(ostream& os, const mint& a) { return os << a.x;}

int main() {
    int n,k;
    cin >> n >> k;
    vector<int> pos,neg,a(n);
    rep(i,n) {
        cin >> a[i];
        if (a[i] < 0) neg.push_back(a[i]);
        else pos.push_back(a[i]);
    }
    bool ok = false;
    int p_size = pos.size(), n_size = neg.size();
    if (p_size > 0) {
        if (n == k) ok = (n_size%2 == 0);
        else ok = true;
    } else {
        ok = (k%2 == 0);
    }
    ll mint = 1;
    if (!ok) {
        sort(a.begin(),a.end(),[](int x, int y){
            return abs(x) < abs(y);
        });
        rep(i,k) {
            ans *= a[i];
            ans = (ans + MOD)%MOD;
        }
    } else {
        sort(pos.begin(),pos.end());
        sort(neg.rbegin(),neg.rend());
        if (k%2 == 1) {
            ans *= pos.back();
            pos.pop_back();
        }
        vector<ll> stk;
        while (pos.size() >= 2) {
            ll x = pos.back(); pos.pop_back();
            x *= pos.back(); pos.pop_back();
            x %= MOD;
            stk.push_back(x);
        }
        while (neg.size() >= 2) {
            ll x = neg.back(); neg.pop_back();
            x *= neg.back(); neg.pop_back();
            x %= MOD;
            stk.push_back(x);
        }
        sort(stk.rbegin(),stk.rend());
        rep(i,k/2) {
            ans *= stk[i];
            ans %= MOD;
        }
    }
    cout << ans << endl;
    return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:64:13: error: 'ans' was not declared in this scope; did you mean 'abs'?
   64 |             ans *= a[i];
      |             ^~~
      |             abs
a.cc:71:13: error: 'ans' was not declared in this scope; did you mean 'abs'?
   71 |             ans *= pos.back();
      |             ^~~
      |             abs
a.cc:89:13: error: 'ans' was not declared in this scope; did you mean 'abs'?
   89 |             ans *= stk[i];
      |             ^~~
      |             abs
a.cc:93:13: error: 'ans' was not declared in this scope; did you mean 'abs'?
   93 |     cout << ans << endl;
      |             ^~~
      |             abs