Language: C++

#include <bits/stdc++.h>
#define rep(i,n)    for(long long i = 0; i < (n) ; ++i)
#define orep(i,n)   for(long long i = 1; i <= (n); ++i)   // one rep
#define drep(i,n)   for(long long i = (n)-1; i >= 0; --i) // down rep
#define srep(i,s,t) for(long long i = (s); i < (t); ++i)  // set rep
#define rng(x)  (x).begin(),(x).end()

  // range
#define rrng(a) (a).rbegin(),(a).rend() // reverse range
#define isin(x,l,r) ((l) <= (x) && (x) < (r))
#define ru(x,y) (((x)+(y)-1)/(y)) // round up
#define fi first
#define se second
#define eb emplace_back
#define fcout cout << fixed << setprecision(15)


using namespace std;
using ll  = long long;
using P   = pair<int,int>;
using vi  = vector<int>;
using vvi = vector<vi>;
using vp  = vector<P>;
using vl  = vector<ll>;

template<typename T> void Yes(T flag) {
  cout << (flag ? "Yes" : "No") << endl;
}

template<typename T> void pv(vector<T> vec) {
  cout << "[";
  for(auto& v : vec) { cout << v << ","; }
  cout << "]" << endl;
}

template<>
void pv(vector<P> vec) {
  cout << "[";
  for(auto& v : vec) {
    cout << "<" << v.fi << ","<< v.se << ">" << ",";
  }
  cout << "]" << endl;
}

template<>
void pv(vector<pair<ll,int>> vec) {
  cout << "[";
  for(auto& v : vec) {
    cout << "<" << v.fi << ","<< v.se << ">" << ",";
  }
  cout << "]" << endl;
}

template<typename T> void pv2(vector<vector<T>> vec) {
  for(auto& v : vec) pv(v);
}

const int mod = 1000000007;
// const int mod = 998244353;

struct mint {
  long long x;
  mint(long long 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 -= a.x) < 0) 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 operator++() const { return mint(*this) += mint(1);}
  mint operator--() const { return mint(*this) -= mint(1);}
  mint pow(long long 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, const 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<pair<ll,int>> v;
  bool flag=true;
  bool zeroflag = false;
  rep(i,n) {
    ll a ; cin >> a;
    if(a>=0) {
      v.eb(pair<ll,int>(a,1));
      flag = false;
      if(a==0) zeroflag=true;
    }
    if(a<0) v.eb(pair<ll,int>(-a,0));
  }
  sort(rrng(v));

  mint ans=mint(1);
  int mp = 0;
  if(zeroflag) {
    rep(i,k) {
      if(v.at(i).se == 0) mp++;
      ans*=mint(v.at(i).fi);
    }
    if(mp%2 != 0) {
      int plmi = v.at(k-1).se;
      bool plmiflag = false;
      // last plus or last minus
        srep(i,k,n) {
          if(v.at(i).se == !plmi) {
            ans/=mint(v.at(k-1).fi);
            ans*=mint(v.at(i).fi);
            plmiflag = true;
            break;
          }
       }
     }
    if(mp%2 != 0 and !plmiflag) ans = 0;
    cout << ans << endl;
    return 0;
  }

  // plus minus, plus all
  ans=mint(1);
  mp = 0;
  rep(i,k) {
    if(v.at(i).se == 0) mp++;
    ans*=mint(v.at(i).fi);
  }
  if(mp%2 != 0) {
    int plmi = v.at(k-1).se;
    bool plmiflag = false;
    // last plus or last minus
      srep(i,k,n) {
        if(v.at(i).se == !plmi) {
          ans/=mint(v.at(k-1).fi);
          ans*=mint(v.at(i).fi);
          plmiflag = true;
          break;
        }
     }
     if(!plmiflag) {
       ans=mint(1);
       sort(rng(v));
       mp=0;
       rep(i,k) {
         if(v.at(i).se == 0) mp++;
         ans*=mint(v.at(i).fi);
       }
       ans=-ans;
    }
  }

  cout << ans << endl;

  return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:135:23: error: 'plmiflag' was not declared in this scope
  135 |     if(mp%2 != 0 and !plmiflag) ans = 0;
      |                       ^~~~~~~~