Language: C++

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+5,mod=1e9+7;
typedef long long ll;
int n,m,k,a[N],b[N],d[N],z;
long double aa[N],bb[N];
ll ma[N],mb[N];
bool cmp(int x,int y){return abs(x)>abs(y);}
int main()
{
    int s;scanf("%d%d",&s,&k);
    while(s--)
    {
        int x;scanf("%d",&x);
        if(x>0) a[++n]=x;
        else if(x<0) b[++m]=-x;
        else z++;
    }
    if(n) sort(a+1,a+1+n,greater<int>());
    if(m) sort(b+1,b+1+m,greater<int>());
    ma[0]=mb[0]=1;
    for(int i=1;i<=n;i++) aa[i]=aa[i-1]+log2((long double)a[i]),ma[i]=ma[i-1]*a[i]%mod;
    for(int i=1;i<=m;i++) bb[i]=bb[i-1]+log2((long double)b[i]),mb[i]=mb[i-1]*b[i]%mod;
    if(n+m<k){printf("0\n");return 0;}
    if(n+m==k)
    {
        if(m&1)
        {
            if(z) printf("0\n");
            else
                printf("%lld\n",(ma[n]*mb[m]%mod*-1+mod)%mod);
        }
        else
        {
            printf("%lld\n",(ma[n]*mb[m]%mod+mod)%mod);
        }
        return 0;
    }
    if(!n)
    {
        if(k&1)
        {
            if(z) printf("0\n");
            else
            {
                reverse(b+1,b+1+m);
                ll ans=1;
                for(int i=1;i<=k;i++) ans=ans*b[i]%mod;
                printf("%lld\n",(ans*-1+mod)%mod);
            }
        }
        else
        {
            printf("%lld\n",(mb[k]+mod)%mod);
        }
        return 0;
    }
    int tot=0;
    for(int i=1;i<=n;i++) d[++tot]=a[i];
    for(int i=1;i<=m;i++) d[++tot]=-b[i];
    sort(d+1,d+1+tot,cmp);
    int t=0;
    for(int i=1;i<=k;i++)
        if(d[i]<0) t^=1;
    if(t)
    {
        bool f1=false,f2=false;
        ll di,dj,ddi,ddj;
        for(int i=k+1;i<=tot;i++)
            if(d[i]>0)
            {
                for(int j=k;j>=1;j--)
                    if(d[j]<0)
                    {
                        di=d[i];dj=-d[j];
                        f1=true;
                        break;
                    }
                break;
            }
        for(int i=k+1;i<=tot;i++)
            if(d[i]<0)
            {
                for(int j=k;j>=1;j--)
                    if(d[j]>0)
                    {
                        dd[i]=d[i];ddj=d[j];
                        f2=true;
                        break;
                    }
                break;
            }
        if(f1&&f2)
        {
            if(di*ddj>dj*ddi)
            {
                for(int i=k+1;i<=tot;i++)
                if(d[i]>0)
                {
                    for(int j=k;j>=1;j--)
                        if(d[j]<0)
                        {
                            swap(d[i],d[j]);
                            break;
                        }
                    break;
                }
            }
            else
            {
                for(int i=k+1;i<=tot;i++)
                if(d[i]<0)
                {
                    for(int j=k;j>=1;j--)
                        if(d[j]>0)
                        {
                            swap(d[i],d[j]);
                            break;
                        }
                    break;
                }
            }
        }
        else if(f1)
        {
            for(int i=k+1;i<=tot;i++)
            if(d[i]>0)
            {
                for(int j=k;j>=1;j--)
                    if(d[j]<0)
                    {
                        swap(d[i],d[j]);
                        break;
                    }
                break;
            }
        }
        else if(f2)
        {
            for(int i=k+1;i<=tot;i++)
                if(d[i]<0)
                {
                    for(int j=k;j>=1;j--)
                        if(d[j]>0)
                        {
                            swap(d[i],d[j]);
                            break;
                        }
                    break;
                }
        }
        else assert(0);
    }
    ll ans=1;
    for(int i=1;i<=k;i++)
        ans=ans*d[i]%mod;
    ans=(ans+mod)%mod;
    printf("%lld\n",ans);
}


Compiler output:
a.cc: In function 'int main()':
a.cc:87:25: error: 'dd' was not declared in this scope; did you mean 'ddj'?
   87 |                         dd[i]=d[i];ddj=d[j];
      |                         ^~
      |                         ddj