#!/usr/bin/env python3
import sys
MOD = 1000000007  # type: int

def solve(N: int, K: int, A: "List[int]"):
    if N == K:
        m = 1
        for a in A:
            m = m*a%MOD
        print(m)
    elif len(list(filter(lambda x: x>=0,A))) == 0 and K%2 == 1:
        A = list(reversed(sorted(A)))
        m = 1
        for i in range(K):
            m = m*A[i]%MOD
        print(m)
    else: # We can make some combination to make positive result in this condition
        m = 1
        B = list(reversed(sorted([(abs(a),a) for a in A]))) # Make a sorted array with absolute value
        lastNegative = 0
        lastPositive = 1
        hadPositive = False
        for i in range(K): # Fill K elements with keeping the result positive. 
            a = B[i][1]
            if a < 0: 
                if lastNegative == 0: # If got a negative value and there are no last negative value kept aside
                    lastNegative = a # Keep this value aside and won't multiply to the result.
                else: # If got a negative value and there are negative value aside last time the loop found negative value, just multiply these 2 values to mul and reset the kept value.
                    m = m*lastNegative*a%MOD 
                    lastNegative = 0
            else:
                if a > 0: # If we got positive value, the value need to be kept aside for once to be swapped another negative value.
                    if hadPositive: # If there is positive value kept aside before, multiply that value to the result
                        m = m*lastPositive%MOD
                    lastPositive = a # The newest value need to be kept aside
                    hadPositive = True
                else:
                    m=m*a%MOD # Only for zero
        if lastNegative == 0: # This assumes the result is already positive. No need to tweak. But need to multiply the last positive value
            print(m*lastPositive%MOD)
        else:
            # Find next negative value after Kth elements
            nextNegative = 0
            for i in range(K,N):
                b = B[i][1]
                if b < 0:
                    nextNegative = b
                    break
            c1 = lastNegative*nextNegative
            nextPositive = 0
            k = K
            while k < N:
                a = B[k][1]
                if a >=0:
                    nextPositive = a
                    break
                k+=1
            c2 = nextPositive*lastPositive
            if not hadPositive: # This array contain some non-negative value. This means the result value could be positive. But if there were no positive values in the first K values sorted by the absolute value, use just next positive value instead of the last negative values.
                m = m*nextPositive%MOD
                print(m)
                exit()
            if c1 > c2:
                m = m*lastNegative*nextNegative%MOD
                print(m)
            else:
                m =m*nextPositive*lastPositive%MOD
                print(m)


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, K, A)

if __name__ == '__main__':
    main()


Measured memory_bytes: 114144