Problem p02616 (python)

Fixed submission:
n,k=map(int,input().split())
A=[int(i) for i in input().split()]
A.sort(key=lambda x:-abs(x))
mod=10**9+7

ans=1
if (max(A)<=0 and k%2==1) or n==k:
    for i in range(k):
        ans=ans*A[n-1-i]%mod
else:
    mi=0
    pl=0
    flg=1
    for i in range(k):
        ans=ans*A[i]%mod
        if A[i]>0:
            pl=A[i]
        if A[i]<0:
            mi=A[i]
            flg*=-1
    if ans==0:
        pass
    elif flg==-1:
        mi2=None
        pl2=None
        for i in range(k,n):
            if A[i]>=0:
                pl2=A[i]
                break
        for i in range(k,n):
            if A[i]<=0:
                mi2=A[i]
                break
        can=0
        if mi!=0 and pl2 is not None and pl!=0 and mi2 is not None:
            if pl2*pl > mi2*mi:
                can = pow(mi,-1,mod)*pl2%mod
            else:
                can = pow(pl,-1,mod)*mi2%mod
        elif mi!=0 and pl2 is not None:
            can = pow(mi,-1,mod)*pl2%mod
        elif pl!=0 and mi2 is not None:
            can = pow(pl,-1,mod)*mi2%mod
        ans=ans*can%mod

print(ans)

Buggy submission (verdict: Runtime Error):
# Problem E - Multiplication 4
def checkio(numbers_array):
    return sorted(numbers_array, key=lambda x: abs(x), reverse=True)

# input
N, K = map(int, input().split())
a_nums = list(map(int, input().split()))

# initialization
a_nums = checkio(a_nums) # 大きい順
a_nums_r = a_nums[::-1] # 小さい順
is_all_minus = all(list(map(lambda x: x<0, a_nums)))
MOD = 10**9 + 7

if is_all_minus:
    ans = 1
    if K%2==0:
        for i in range(K):
            ans *= a_nums[i] % MOD
            ans %= MOD
        print(ans%MOD)
    else:
        for i in range(K):
            ans *= a_nums_r[i] % MOD
            ans %= MOD
        print(ans%MOD)
else: # 1つでも非負整数が含まれている場合
    plus_list = []
    minus_list = []
    plus_count = 0
    minus_count = 0
    cur_point = 1
    # 絶対値順に数をとってみる
    for i in range(K):
        a = a_nums.pop(0)
        cur_point *= a % MOD # 暫定の最大値
        if a>=0:
            plus_list.append(a)
        else:
            minus_list.append(a)
            minus_count += 1
    if minus_count%2==0: # 負の個数が偶数であればそれが答え
        ans = 1
        for p in plus_list:
            ans *= p % MOD
            ans %= MOD
        for m in minus_list:
            ans *= m % MOD
            ans %= MOD
        print(ans%MOD)
    else: # 負の個数が奇数の時
        # 正の数を加えてみる
        tmp_1 = (-1) * float('INF')
        tmp_2 = (-1) * float('INF')
        tmp_3 = 1
        if any(list(map(lambda x: x>=0, a_nums))):
            # 正の数を取得
            tmp = 0
            for a in a_nums:
                if a>=0:
                    tmp = a
                    break
            m = minus_list.pop()
            p = tmp
            tmp_1 = abs(p / m)

        if any(list(map(lambda x: x<0, a_nums))):
            # 負の数を取得
            tmp = 0
            for a in a_nums:
                if a<0:
                    tmp = a
                    break
            m = tmp
            p = plus_list.pop()
            tmp_2 = abs(m / p)

        if tmp_3>=tmp_1 and tmp_3>=tmp_2:
            print(cur_point)
        elif tmp_1>=tmp_2 and tmp_1>=tmp_3:
            cur_point *= tmp_1
            print(cur_point)
        else:
            cur_point *= tmp_2
            print(cur_point)

Buggy submission (verdict: Runtime Error):
# Problem E - Multiplication 4
def checkio(numbers_array):
    return sorted(numbers_array, key=lambda x: abs(x), reverse=True)

# input
N, K = map(int, input().split())
a_nums = list(map(int, input().split()))

# initialization
a_nums = checkio(a_nums) # 大きい順
a_nums_r = a_nums[::-1] # 小さい順
is_all_minus = all(list(map(lambda x: x<0, a_nums)))
MOD = 10**9 + 7

if is_all_minus:
    ans = 1
    if K%2==0:
        for i in range(K):
            ans *= a_nums[i] % MOD
            ans %= MOD
        print(ans%MOD)
    else:
        for i in range(K):
            ans *= a_nums_r[i] % MOD
            ans %= MOD
        print(ans%MOD)
else: # 1つでも非負整数が含まれている場合
    plus_list = []
    minus_list = []
    plus_count = 0
    minus_count = 0
    cur_point = 1
    # 絶対値順に数をとってみる
    for i in range(K):
        a = a_nums.pop(0)
        cur_point *= a % MOD # 暫定の最大値
        if a>=0:
            plus_list.append(a)
        else:
            minus_list.append(a)
            minus_count += 1
    if minus_count%2==0: # 負の個数が偶数であればそれが答え
        ans = 1
        for p in plus_list:
            ans *= p % MOD
            ans %= MOD
        for m in minus_list:
            ans *= m % MOD
            ans %= MOD
        print(ans%MOD)
    else: # 負の個数が奇数の時
        # 正の数を加えてみる
        tmp_1 = (-1) * float('INF')
        tmp_2 = (-1) * float('INF')
        tmp_3 = 1
        if any(list(map(lambda x: x>=0, a_nums))):
            # 正の数を取得
            tmp = 0
            for a in a_nums:
                if a>=0:
                    tmp = a
                    break
            m = minus_list[-1]
            p = tmp
            tmp_1 = abs(p / m)

        if any(list(map(lambda x: x<0, a_nums))):
            # 負の数を取得
            tmp = 0
            for a in a_nums:
                if a<0:
                    tmp = a
                    break
            m = tmp
            p = plus_list[-1]
            tmp_2 = abs(m / p)

        if tmp_3>=tmp_1 and tmp_3>=tmp_2:
            print(cur_point)
        elif tmp_1>=tmp_2 and tmp_1>=tmp_3:
            cur_point *= tmp_1
            print(cur_point)
        else:
            cur_point *= tmp_2
            print(cur_point)