Language: C++

#include<iostream>
#include<stdio.h>
#include<string>
#include<vector>
#include<queue>
#include <cmath>
#include<algorithm>
#include<stack>
#include<map>
#include<ctime>
#define rep(i,n) for (int i=0;i<n;i++)
//#include <bits/stdc++.h>
using namespace std;

const long int INF=1000000007;

int main(){
    int N,K;
    cin>>N>>K;
    vector<long int> A(N),B,C;
    bool z=false;
    rep(i,N){
        cin>>A[i];
        if (A[i]>0) B.push_back(A[i]);
        if (A[i]==0) z=true;
        if (A[i]<0) C.push_back(A[i]); 
    }
    sort(B.begin(),B.end());
    sort(C.begin(),C.end());
    int p=B.size(),q=C.size();
    long long ans=1;
    if (p==0){
        if (q<K){
            cout<<0<<endl;
            return 0;
        }
        else{
            if (K%2==0){
                rep(i,K){
                    ans=ans*(-C[i]);
                    ans=ans%INF;
                }
                cout<<ans<<endl;
                return 0;
            } 
            else{
                if (z){
                    cout<<0;
                    return 0;
                }
                rep(i,K){
                    ans=ans*(-C[q-1-i]);
                    ans=ans%INF;
                }
                cout<<INF-ans<<endl;
                return 0;
            }
        }
    }
    if (N==K){
        rep(i=0;i<N;i++){
            ans=ans*abs(A[i]);
            ans=ans%INF;
       }
        if (q%2==1) cout<<INF+ans<<endl;
        else cout<<ans<<endl;
    }
    int s,t;
    if (p+q<K){
        cout<<0<<endl;
        return 0;
    }
    if (p>=K){
        t=p-K;
        s=0;
        while(true){
            if (t+1<p&&s+1<q){
                long long n=B[t]*B[t+1];
                long long m=C[s]*C[s+1];
                if (n<m){
                    s+=2;
                    t+=2;
                }
                else break;
            }
            else break;
        }
        for (int i=t;i<p;i++){
            ans=ans*B[i];
            ans=ans%INF;
        }
        for (int i=0;i<s;i++){
            ans=ans*(-C[i]);
            ans=ans%INF;
        }
        cout<<ans<<endl;
        return 0;
    }
    if ((K-p)%2==0){
        t=0;
        s=K-p;
        while(true){
            if (t+1<p&&s+1<q){
                long long n=B[t]*B[t+1];
                long long m=C[s]*C[s+1];
                if (n<m){
                    s+=2;
                    t+=2;
                }
                else break;
            }
            else break;
        }
        for (int i=t;i<p;i++){
            ans=ans*B[i];
            ans=ans%INF;
        }
        for (int i=0;i<s;i++){
            ans=ans*(-C[i]);
            ans=ans%INF;
        }
        cout<<ans<<endl;
        return 0;
    }
    else{
        t=1;
        s=K-p+1;
        while(true){
            if (t+1<p&&s+1<q){
                long long n=B[t]*B[t+1];
                long long m=C[s]*C[s+1];
                if (n<m){
                    s+=2;
                    t+=2;
                }
                else break;
            }
            else break;
        }
        for (int i=t;i<p;i++){
            ans=ans*B[i];
            ans=ans%INF;
        }
        for (int i=0;i<s;i++){
            ans=ans*(-C[i]);
            ans=ans%INF;
        }
        cout<<ans<<endl;
        return 0;
    }
    
    return 0;
}

Compiler output:
a.cc:61:24: error: macro "rep" requires 2 arguments, but only 1 given
   61 |         rep(i=0;i<N;i++){
      |                        ^
a.cc:11:9: note: macro "rep" defined here
   11 | #define rep(i,n) for (int i=0;i<n;i++)
      |         ^~~
a.cc: In function 'int main()':
a.cc:61:9: error: 'rep' was not declared in this scope
   61 |         rep(i=0;i<N;i++){
      |         ^~~