Problem p02616 (python)

Fixed submission:
def mergesort(l):
    n = len(l)
    if(n == 0):
        return 0
    elif(n == 1):
        return l
    elif(n <= 2):
        if(l[0] > l[1]):
            return [l[1],l[0]]
        else:
            return l
    else:
        tmp1 = mergesort(l[:n//2])
        tmp2 = mergesort(l[n//2:])
        x = n//2
        y = n-x
        l = []
        for i in range(n):
            if(tmp1[-x] < tmp2[-y]):
                l.append(tmp1[-x])
                x -= 1
            else:
                l.append(tmp2[-y])
                y -= 1
            if(x*y == 0):
                if(x == 0):
                    l += tmp2[-y:]
                else:
                    l += tmp1[-x:]
                break
        return l
n,k = map(int,input().split())
a = list(map(int,input().split()))
b = []
c = []
s,t,u = 0,0,0
for i in range(n):
    if(a[i] < 0):
        t += 1
        c.append(a[i])
    else:
        s += 1
        b.append(a[i])
b = mergesort(b)
c = mergesort(c)
p = 1
if(s == 0 and k%2 == 1):
    for i in range(1,k+1):
        p *= c[-i]
        p %= 1000000007
else:
    if(n == k):
        for i in range(n):
            p *= a[i]
            p %= 1000000007
    else:
        if(k%2 == 1):
            s-=1
            p = b[s]
            k -= 1
        while(k>0):
            if(s >= 2 and t >= u+2):
                if(b[s-1]*b[s-2] > c[u]*c[u+1]):
                    p *= b[s-1]*b[s-2]
                    p %= 1000000007
                    s -= 2
                else:
                    p *= c[u]*c[u+1]
                    p %= 1000000007
                    u += 2
            elif(s <= 1):
                p *= c[u]*c[u+1]
                p %= 1000000007
                u += 2
            else:
                p *= b[s-1]*b[s-2]
                p %= 1000000007
                s -= 2
            k -= 2
print(p)

Buggy submission (verdict: Runtime Error):
def mergesort(l):
    n = len(l)
    if(n == 0):
        return 0
    elif(n == 1):
        return l
    elif(n <= 2):
        if(l[0] > l[1]):
            return [l[1],l[0]]
        else:
            return l
    else:
        tmp1 = mergesort(l[:n//2])
        tmp2 = mergesort(l[n//2:])
        x = n//2
        y = n-x
        l = []
        for i in range(n):
            if(tmp1[-x] < tmp2[-y]):
                l.append(tmp1[-x])
                x -= 1
            else:
                l.append(tmp2[-y])
                y -= 1
            if(x*y == 0):
                if(x == 0):
                    l += tmp2[-y:]
                else:
                    l += tmp1[-x:]
                break
        return l
n,k = map(int,input().split())
a = list(map(int,input().split()))
b = []
c = []
s,t,u = 0,0,0
for i in range(n):
    if(a[i] < 0):
        t += 1
        c.append(a[i])
    else:
        s += 1
        b.append(a[i])
b = mergesort(b)
c = mergesort(c)
p = 1
if(s == 0 and k%2 == 1):
    for i in range(1,k+1):
        p *= c[-i]
        p %= 1000000007
else:
    if(k%2 == 1):
        s-=1
        p = b[s]
        k -= 1
    while(k>0):
        if(s >= 2 and t >= u+2):
            if(b[s-1]*b[s-2] > c[u]*c[u+1]):
                p *= b[s-1]*b[s-2]
                p %= 1000000007
                s -= 2
            else:
                p *= c[u]*c[u+1]
                p %= 1000000007
                u += 2
        elif(s <= 1):
            p *= c[u]*c[u+1]
            p %= 1000000007
            u += 2
        else:
            p *= b[s-1]*b[s-2]
            p %= 1000000007
            s -= 2
        k -= 2
print(p)

Buggy submission (verdict: Runtime Error):
def mergesort(l):
    n = len(l)
    if(n == 0):
        return 0
    elif(n == 1):
        return l
    elif(n <= 2):
        if(l[0] > l[1]):
            return [l[1],l[0]]
        else:
            return l
    else:
        tmp1 = mergesort(l[:n//2])
        tmp2 = mergesort(l[n//2:])
        x = n//2
        y = n-x
        l = []
        for i in range(n):
            if(tmp1[-x] < tmp2[-y]):
                l.append(tmp1[-x])
                x -= 1
            else:
                l.append(tmp2[-y])
                y -= 1
            if(x*y == 0):
                if(x == 0):
                    l += tmp2[-y:]
                else:
                    l += tmp1[-x:]
                break
        return l
n,k = map(int,input().split())
a = list(map(int,input().split()))
b = []
c = []
s,t,u = 0,0,0
for i in range(n):
    if(a[i] < 0):
        t += 1
        c.append(a[i])
    else:
        s += 1
        b.append(a[i])
b = mergesort(b)
c = mergesort(c)
p = 1
if(s == 0 and k%2 == 1):
    for i in range(1,k+1):
        p *= c[-i]
        p %= 1000000007
else:
    if(k%2 == 1):
        s-=1
        p = b[s]
        k -= 1
    while(k>0):
        if(s > 0 and t > u):
            if(b[s-1]*b[s-2] > c[u]*c[u+1]):
                p *= b[s-1]*b[s-2]
                p %= 1000000007
                s -= 2
            else:
                p *= c[u]*c[u+1]
                p %= 1000000007
                u += 2
        elif(s == 0):
            p *= c[u]*c[u+1]
            p %= 1000000007
            u += 2
        else:
            p *= b[s-1]*b[s-2]
            p %= 1000000007
            s -= 2
        k -= 2
print(p)