Problem:
Score : 500 points

Problem Statement

Given are N integers A_1,\ldots,A_N.

We will choose exactly K of these elements. Find the maximum possible product of the chosen elements.

Then, print the maximum product modulo (10^9+7), using an integer between 0 and 10^9+6 (inclusive).

Constraints

1 \leq K \leq N \leq 2\times 10^5

|A_i| \leq 10^9

Input

Input is given from Standard Input in the following format:

N K
A_1 \ldots A_N

Output

Print the maximum product modulo (10^9+7), using an integer between 0 and 10^9+6 (inclusive).

Sample Input 1

4 2
1 2 -3 -4

Sample Output 1

12

The possible products of the two chosen elements are 2, -3, -4, -6, -8, and 12, so the maximum product is 12.

Sample Input 2

4 3
-1 -2 -3 -4

Sample Output 2

1000000001

The possible products of the three chosen elements are -24, -12, -8, and -6, so the maximum product is -6.

We print this value modulo (10^9+7), that is, 1000000001.

Sample Input 3

2 1
-1 1000000000

Sample Output 3

1000000000

The possible products of the one chosen element are -1 and 1000000000, so the maximum product is 1000000000.

Sample Input 4

10 10
1000000000 100000000 10000000 1000000 100000 10000 1000 100 10 1

Sample Output 4

999983200

Be sure to print the product modulo (10^9+7).

Solution in C# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using static Scanner;

class Program
{
    static void Main() => new Program().Solve();

    public void Solve()
    {
        int n = ReadInt();
        int k = ReadInt();
        int[] a = ReadInt(n).OrderBy(x => x).ToArray();
        int[] neg = a.TakeWhile(x => x < 0).ToArray();
        int[] pos = a.SkipWhile(x => x < 0).Reverse().ToArray();

        ModInt prod = 1;
        if (neg.Length == 0 || n == k)
        {
            foreach (var x in a.TakeLast(k)) prod *= x;
            Console.WriteLine((int)prod);
            return;
        }

        if (pos.Length == 0)
        {
            if (k % 2 == 0)
            {
                foreach (var x in a.Take(k)) prod *= x;
            }
            else
            {
                foreach (var x in a.TakeLast(k)) prod *= x;
            }
            Console.WriteLine((int)prod);
            return;
        }

        for (int i = 0; i <= neg.Length; i += 2)
        {
            if (k - i > pos.Length) continue;
            if (i + 1 < neg.Length && k - i - 2 >= 0 && (long)pos[k - i - 2] * pos[k - i - 1] < (long)neg[i] * neg[i + 1]) continue;
            prod = 1;
            foreach (var x in pos.Take(k - i)) prod *= x;
            foreach (var x in neg.Take(i)) prod *= -x;
            break;
        }
        Console.WriteLine((int)prod);
    }
}

static class Scanner
{
    static Queue<string> que = new Queue<string>();
    public static string Read() { if (!que.Any()) foreach (var s in Console.ReadLine().Split()) que.Enqueue(s); return que.Dequeue(); }
    public static int ReadInt() => int.Parse(Read());
    public static long ReadLong() => long.Parse(Read());
    public static double ReadDouble() => double.Parse(Read());
    public static decimal ReadDecimal() => decimal.Parse(Read());
    public static IEnumerable<string> Read(long n) { for (long i = 0; i < n; i++) yield return Read(); }
    public static IEnumerable<int> ReadInt(long n) => Read(n).Select(int.Parse);
    public static IEnumerable<long> ReadLong(long n) => Read(n).Select(long.Parse);
    public static IEnumerable<double> ReadDouble(long n) => Read(n).Select(double.Parse);
    public static IEnumerable<decimal> ReadDecimal(long n) => Read(n).Select(decimal.Parse);
}

struct ModInt
{
    const int MOD = 1000000007;
    long value;
    static List<ModInt> fact = new List<ModInt> { 1 };
    public ModInt(long value) => this.value = value;
    public static implicit operator ModInt(long a) => new ModInt(a % MOD + (a < 0 ? MOD : 0));
    public static explicit operator int(ModInt a) => (int)a.value;
    public static ModInt operator +(ModInt a, ModInt b) => a.value + b.value;
    public static ModInt operator -(ModInt a, ModInt b) => a.value - b.value;
    public static ModInt operator *(ModInt a, ModInt b) => a.value * b.value;
    public static ModInt operator /(ModInt a, ModInt b) => a * Inv(b);
    public static ModInt Pow(ModInt a, int n) { if (n == 0) return 1; if (n == 1) return a; var p = Pow(a, n / 2); return p * p * Pow(a, n % 2); }
    public static ModInt Inv(ModInt a) => Pow(a, MOD - 2);
    public static ModInt Fact(int n) { for (int i = fact.Count; i <= n; i++) fact.Add(fact[^1] * i); return fact[n]; }
    public static ModInt Npr(int n, int r) => Fact(n) / Fact(n - r);
    public static ModInt Ncr(int n, int r) => Fact(n) / Fact(n - r) / Fact(r);
    public static ModInt Nhr(int n, int r) => Fact(n + r - 1) / Fact(n - 1) / Fact(r);
}