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: Wrong Answer)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.Contracts;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Xml.Schema;
using System.Threading;
using System.Diagnostics.CodeAnalysis;
using System.Security.Cryptography;
using System.IO.Pipes;
using Microsoft.Win32;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.CodeDom;

namespace AtCoder
{
    public static class Ex
    {
        public static List<string> FastSort(this List<string> s) { s.Sort(StringComparer.OrdinalIgnoreCase); return s.ToList(); }
        public static void yesno(this bool b) { Console.WriteLine(b ? "yes" : "no"); }
        public static void YesNo(this bool b) { Console.WriteLine(b ? "Yes" : "No"); }
        public static void YESNO(this bool b) { Console.WriteLine(b ? "YES" : "NO"); }
        public static int BuildBitCount(this int bits)
        {
            bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
            bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
            bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
            bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
            return (bits & 0x0000ffff) + (bits >> 16 & 0x0000ffff);
        }
    }

    partial class AtCoder
    {
        public string GetStr() { return Console.ReadLine().Trim(); }
        public char GetChar() { return Console.ReadLine().Trim()[0]; }
        public int GetInt() { return int.Parse(Console.ReadLine().Trim()); }
        public long GetLong() { return long.Parse(Console.ReadLine().Trim()); }
        public double GetDouble() { return double.Parse(Console.ReadLine().Trim()); }
        public string[] GetStrArray() { return Console.ReadLine().Trim().Split(' '); }
        public int[] GetIntArray() { return Console.ReadLine().Trim().Split(' ').Select(int.Parse).ToArray(); }
        public long[] GetLongArray() { return Console.ReadLine().Trim().Split(' ').Select(long.Parse).ToArray(); }
        public char[] GetCharArray() { return Console.ReadLine().Trim().Split(' ').Select(char.Parse).ToArray(); }
        public double[] GetDoubleArray() { return Console.ReadLine().Trim().Split(' ').Select(double.Parse).ToArray(); }
        public T[][] CreateJaggedArray<T>(int H, int W, T value) { return Enumerable.Repeat(0, H).Select(s => Enumerable.Repeat(value, W).ToArray()).ToArray(); }
        public int[][] GetIntJaggedArray(int N) { return Enumerable.Repeat(0, N).Select(s => GetIntArray().ToArray()).ToArray(); }
        public long[][] GetLongJaggedArray(int N) { return Enumerable.Repeat(0, N).Select(s => GetLongArray().ToArray()).ToArray(); }
        public char[][] GetCharJaggedArray(int N) { return Enumerable.Repeat(0, N).Select(s => GetStr().ToCharArray()).ToArray(); }
        public double[][] GetDoubleJaggedArray(int N) { return Enumerable.Repeat(0, N).Select(s => GetDoubleArray()).ToArray(); }

        Dictionary<int, List<int>> GetUnweightedAdjacencyList(int N, int M, bool isDirected, bool isNode_0indexed)
        {
            var dic = new Dictionary<int, List<int>>();
            foreach (var e in Enumerable.Range(0, N)) { dic.Add(e, new List<int>()); }
            for (int i = 0; i < M; i++)
            {
                var input = GetIntArray();
                var a = isNode_0indexed ? input[0] : input[0] - 1;
                var b = isNode_0indexed ? input[1] : input[1] - 1;
                dic[a].Add(b);
                if (isDirected == false) dic[b].Add(a);
            }
            return dic;
        }
        Dictionary<int, List<Edge>> GetWeightedAdjacencyList(int N, int M, bool isDirected, bool isNode_0indexed)
        {
            var dic = new Dictionary<int, List<Edge>>();
            foreach (var e in Enumerable.Range(0, N)) { dic.Add(e, new List<Edge>()); }
            for (int i = 0; i < M; i++)
            {
                var input = GetIntArray();
                var a = isNode_0indexed ? input[0] : input[0] - 1;
                var b = isNode_0indexed ? input[1] : input[1] - 1;
                var c = input[2];
                dic[a].Add(new Edge(b, c));
                if (isDirected == false) dic[b].Add(new Edge(a, c));
            }
            return dic;
        }

        bool eq<T, U>() => typeof(T).Equals(typeof(U));
        T ct<T, U>(U a) => (T)Convert.ChangeType(a, typeof(T));
        T cv<T>(string s) => eq<T, int>() ? ct<T, int>(int.Parse(s))
                           : eq<T, long>() ? ct<T, long>(long.Parse(s))
                           : eq<T, double>() ? ct<T, double>(double.Parse(s))
                           : eq<T, char>() ? ct<T, char>(s[0])
                                             : ct<T, string>(s);
        void Multi<T>(out T a) => a = cv<T>(GetStr());
        void Multi<T, U>(out T a, out U b)
        {
            var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]);
        }
        void Multi<T, U, V>(out T a, out U b, out V c)
        {
            var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]);
        }
        void Multi<T, U, V, W>(out T a, out U b, out V c, out W d)
        {
            var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); d = cv<W>(ar[3]);
        }
        void Multi<T, U, V, W, X>(out T a, out U b, out V c, out W d, out X e)
        {
            var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); d = cv<W>(ar[3]); e = cv<X>(ar[4]);
        }
        void Multi<T, U, V, W, X, Y>(out T a, out U b, out V c, out W d, out X e, out Y f)
        {
            var ar = GetStrArray(); a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); d = cv<W>(ar[3]); e = cv<X>(ar[4]); f = cv<Y>(ar[5]);
        }
    }

    class Edge
    {
        public Edge(int to, long length)
        {
            To = to;
            Length = length;
        }
        public int To { get; set; }
        public long Length { get; set; }
    }

    class Comparable : IComparable<Comparable>
    {
        public Comparable(int value)
        {
            this.value = value;
        }

        public int value;
        public int CompareTo(Comparable c)
        {
            return this.value < c.value ? 1 : this.value == c.value ? 0 : -1;
        }
    }



    partial class Program
    {
        static void Main()
        {
            Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
            new AtCoder().Solve();
            Console.Out.Flush();
            Console.Read();
        }
    }

    public partial class AtCoder
    {
        class Pair : IComparable<Pair>
        {
            public Pair(int node, long cost)
            {
                Node = node; Cost = cost;
            }
            public int Node { get; set; }
            public long Cost { get; set; }
            public int CompareTo(Pair p)
            {
                return p.Cost > Cost ? 1 : -1;
            }
        }

        class PriorityQueue<T> where T : IComparable<T>
        {
            public PriorityQueue()
            {
                buffer = new List<T>();
            }

            List<T> buffer;
            public void Enqueue(T elem)
            {
                int n = buffer.Count;
                buffer.Add(elem);
                while (n != 0)
                {
                    int i = (n - 1) / 2;
                    if (buffer[n].CompareTo(buffer[i]) > 0)
                    {
                        T temp = buffer[n];
                        buffer[n] = buffer[i];
                        buffer[i] = temp;
                    }
                    n = i;
                }
            }

            public T Dequeue()
            {
                var t = Peek();
                Pop();
                return t;
            }

            void Pop()
            {
                int n = buffer.Count - 1;
                buffer[0] = buffer[n];
                buffer.RemoveAt(n);
                for (int i = 0, j; (j = 2 * i + 1) < n;)
                {
                    if ((j != n - 1) && (buffer[j].CompareTo(buffer[j + 1]) < 0)) { j++; }
                    if (buffer[i].CompareTo(buffer[j]) < 0)
                    {
                        T temp = buffer[j];
                        buffer[j] = buffer[i];
                        buffer[i] = temp;
                    }
                    i = j;
                }
            }

            public T Peek()
            {
                return buffer[0];
            }

            public int Count()
            {
                return buffer.Count;
            }
        }

        public void Solve()
        {
            long mod = 1000000007;
            int N; int K;
            Multi(out N, out K);
            var A = GetLongArray();

            if (N == K)
            {
                var ans = A.Aggregate(1L, (long x, long y) => (x * y % mod + mod) % mod);
                Console.WriteLine(ans);
                return;
            }

            if (A.All(a => a < 0) && K % 2 == 1)
            {
                var ans = A.OrderBy(o => Math.Abs(o)).Take(K).Aggregate(1L, (long x, long y) => (x * y % mod + mod) % mod);
                Console.WriteLine(ans);
                return;
            }

            A = A.OrderByDescending(o => Math.Abs(o)).ToArray();

            var p = 0;
            var m = 0;
            var z = 0;
            long lastPlus = -1;
            var lastPlusIndex = -1;
            long lastMinus = 0;
            var lastMinusIndex = 0;


            for (int i = 0; i < K; i++)
            {
                if (A[i] > 0)
                {
                    p++;
                    lastPlus = A[i];
                    lastPlusIndex = i;
                }
                else if (A[i] < 0)
                {
                    m++;
                    lastMinus = A[i];
                    lastMinusIndex = i;
                }
                else z++;
            }

            if (z > 0 || m % 2 == 0)
            {
                var ans = A.Take(K).Aggregate(1L, (long x, long y) => (x * y % mod + mod) % mod);
                Console.WriteLine(ans);
                return;
            }

            long firstPlus = 0;
            long firstMinus = 0;
            for (int i = K; i < N; i++)
            {
                if (A[i] >= 0)
                {
                    firstPlus = A[i];
                    break;
                }
            }
            for (int i = K; i < N; i++)
            {
                if (A[i] < 0)
                {
                    firstMinus = A[i];
                    break;
                }
            }

            if (lastPlusIndex == -1 || lastPlus * firstPlus > lastMinus * lastPlus)
            {
                A[lastMinusIndex] = firstPlus;
            }
            else
            {
                A[lastPlusIndex] = firstMinus;
            }
            var fans = A.Take(K).Aggregate(1L, (long x, long y) => (x * y % mod + mod) % mod);
            Console.WriteLine(fans);

        }
    }
}