Problem p02616 (python)

Fixed submission:
n,k = map(int,input().split())
lis = list(map(int,input().split()))
def shinchan(before,after):
    an = 1
    key = 1
    for i in range(k):
        if key and abs(lis[i]) == before:
            an *= after
            key = 0
        else:
            an *= abs(lis[i])
        an %= 10 ** 9 + 7
    return an
plus = 0
minus = 0
zero = 0
p,m = [],[]
for num in lis:
    if num > 0:
        plus += 1
        p.append(num)
    elif num < 0:
        minus += 1
        m.append(num)
    else:
        zero += 1
p.sort(reverse=True)
m.sort(key = lambda x:abs(x),reverse=True)
lis.sort(key=lambda x:abs(x),reverse=True)
zissaiplus = 0
zissaiminus = 0
zissaizero = 0
for i in range(k):
    if lis[i] > 0:
        zissaiplus += 1
    elif lis[i] < 0:
        zissaiminus += 1
    else:
        zissaizero += 1
if zissaizero > 0:
    print(0)
    exit()
if zissaiminus % 2 == 0:
    ans = shinchan(-1,-1)
    print(ans)
    exit()
else:
    key1,key2 = 0,0
    if zissaiminus > 0 and plus-zissaiplus > 0:
        be1 = -m[zissaiminus-1]
        af1 = p[zissaiplus]
        key1 = 1
    if zissaiplus > 0 and minus-zissaiminus > 0:
        be2 = p[zissaiplus-1]
        af2 = -m[zissaiminus]
        key2 = 1
    if key1 == 1 and key2 == 1:
        if be2 * af1 >= be1 * af2:
            ans = shinchan(be1,af1)
        else:
            ans = shinchan(be2,af2)
    elif key1 or key2:
        if key1:
            ans = shinchan(be1,af1)
        else:
            ans = shinchan(be2,af2)
    else:
        if zero > 0:
            print(0)
            exit()
        lis.sort(key=lambda x:abs(x))
        ans = -shinchan(0,0)+10 ** 9 + 7
    print(ans)

Buggy submission (verdict: Runtime Error):
n,k = map(int,input().split())
lis = list(map(int,input().split()))
def shinchan(before,after):
    an = 1
    key = 1
    for i in range(k):
        if key and abs(lis[i]) == before:
            an *= after
            key = 0
        else:
            an *= abs(lis[i])
        an %= 10 ** 9 + 7
    return an
plus = 0
minus = 0
zero = 0
p,m = [],[]
for num in lis:
    if num > 0:
        plus += 1
        p.append(num)
    elif num < 0:
        minus += 1
        m.append(num)
    else:
        zero += 1
p.sort(reverse=True)
m.sort(key = lambda x:abs(x),reverse=True)
lis.sort(key=lambda x:abs(x),reverse=True)
zissaiplus = 0
zissaiminus = 0
zissaizero = 0
for i in range(k):
    if lis[i] > 0:
        zissaiplus += 1
    elif lis[i] < 0:
        zissaiminus += 1
    else:
        zissaizero += 1
if zissaizero > 0:
    print(0)
    exit()
if zissaiminus % 2 == 0:
    ans = shinchan(-1,-1)
    print(ans)
    exit()
else:
    key1,key2 = 0,0
    if zissaiminus > 0 and plus-zissaiplus > 0:
        be1 = -m[zissaiminus-1]
        af1 = p[zissaiplus]
        key1 = 1
    if zissaiplus > 0 and minus-zissaiminus > 0:
        be2 = p[zissaiplus-1]
        af2 = -m[zissaiplus]
        key2 = 1
    if key1 == 1 and key2 == 1:
        if be2 * af1 >= be1 * af2:
            ans = shinchan(be1,af1)
        else:
            ans = shinchan(be2,af2)
    elif key1 or key2:
        if key1:
            ans = shinchan(be1,af1)
        else:
            ans = shinchan(be2,af2)
    else:
        if zero > 0:
            print(0)
            exit()
        lis.sort(key=lambda x:abs(x))
        ans = -shinchan(0,0)+10 ** 9 + 7
    print(ans)

Buggy submission (verdict: Runtime Error):
n,k = map(int,input().split())
lis = list(map(int,input().split()))
def shinchan(before,after):
    ans = 1
    key = 1
    for i in range(k):
        if key and abs(lis[i]) == before:
            ans *= after
            key = 0
        else:
            ans *= abs(lis[i])
        ans %= 10 ** 9 + 7
    return ans
plus = 0
minus = 0
zero = 0
p,m = [],[]
for num in lis:
    if num > 0:
        plus += 1
        p.append(num)
    elif num < 0:
        minus += 1
        m.append(num)
    else:
        zero += 1
p.sort(reverse=True)
m.sort(key = lambda x:abs(x),reverse=True)
lis.sort(key=lambda x:abs(x),reverse=True)
zissaiplus = 0
zissaiminus = 0
zissaizero = 0
for i in range(k):
    if lis[i] > 0:
        zissaiplus += 1
    elif lis[i] < 0:
        zissaiminus += 1
    else:
        zissaizero += 1
if zissaizero > 0:
    print(0)
    exit()
if zissaiminus % 2 == 0:
    ans = shinchan(-1,-1)
    print(ans)
    exit()
else:
    key1,key2 = 0,0
    if zissaiminus > 0 and plus-zissaiplus > 0:
        be1 = -m[zissaiminus-1]
        af1 = p[zissaiplus]
        key1 = 1
    if zissaiplus > 0 and minus-zissaiminus > 0:
        be2 = p[zissaiplus-1]
        af2 = -m[zissaiplus]
        key2 = 1
    if key1 == 1 and key2 == 1:
        if be2 * af1 >= be1 * af2:
            ans = shinchan(be1,af1)
        else:
            ans = shinchan(be2,af2)
    elif key1 or key2:
        if key1:
            ans = shinchan(be1,af1)
        else:
            ans = shinchan(be2,af2)
    else:
        if zero > 0:
            print(0)
            exit()
        lis.sort(key=lambda x:abs(x))
        ans = -shinchan(0,0)+10 ** 9 + 7
    print(ans)

Buggy submission (verdict: Runtime Error):
n,k = map(int,input().split())
lis = list(map(int,input().split()))
def shinchan(before,after):
    ans = 1
    key = 1
    for i in range(k):
        if key and abs(lis[i]) == before:
            ans *= after
            key = 0
        else:
            ans *= abs(lis[i])
        ans %= 10 ** 9 + 7
    return ans
plus = 0
minus = 0
zero = 0
p,m = [],[]
for num in lis:
    if num > 0:
        plus += 1
        p.append(num)
    elif num < 0:
        minus += 1
        m.append(num)
    else:
        zero += 1
p.sort(reverse=True)
m.sort(key = lambda x:abs(x),reverse=True)
lis.sort(key=lambda x:abs(x),reverse=True)
zissaiplus = 0
zissaiminus = 0
zissaizero = 0
for i in range(k):
    if lis[i] > 0:
        zissaiplus += 1
    elif lis[i] < 0:
        zissaiminus += 1
    else:
        zissaizero += 1
if zissaizero > 0:
    print(0)
    exit()
if zissaiminus % 2 == 0:
    ans = shinchan(-1,-1)
    print(ans)
    exit()
else:
    key1,key2 = 0,0
    if zissaiminus > 0 and plus-zissaiplus > 0:
        be1 = -m[zissaiminus-1]
        af1 = p[zissaiplus]
        key1 = 1
    if zissaiplus > 0 and minus-zissaiminus > 0:
        be2 = p[zissaiplus-1]
        af2 = -m[zissaiplus]
        key2 = 1
    if key1 == 1 and key2 == 1:
        if be2 * af1 >= be1 * af2:
            ans = shinchan(be1,af1)
        else:
            ans = shinchan(be2,af2)
    elif key1 or key2:
        if key1:
            ans = shinchan(be1,af1)
        else:
            ans = shinchan(be2,af2)
    else:
        lis.sort(key=lambda x:abs(x))
        ans = -shinchan(0,0)+10 ** 9 + 7
    print(ans)