Parity Permutation Problem

from math import factorial

def test():
modVal = (10 ** 9) + 7
N, K = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]

if N == 1:
    print(1)
    return

evenCount = 0
oddCount = 0
for Ai in A:
    if Ai % 2 == 0: evenCount += 1
    else: oddCount += 1

if K == 0:
    if evenCount == 0 or oddCount == 0:
        print(factorial(N))
    else:
        print(0)

elif N % 2 == 0:
    if evenCount == oddCount:
        f = factorial(N//2)
        print((2 * f * f) % modVal)
    else:
        print(0)
else:
    if abs(evenCount - oddCount) == 1:
        f = factorial(N//2)
        print((f * f * ((N//2) + 1)) % modVal)
    else:
        print(0)

for _ in range(int(input())):
test()

The above code has the exact same logic as several other solutions I found. But for some reason it is failing the testcases… I can’t find a testcase where it is not right. Can anyone see any bugs in the code ?

Well, I can see there are some syntax errors with your test function, You can try this code to fix the error you are getting.

from math import factorial

def test():
    modVal = (10 ** 9) + 7
    N, K = [int(i) for i in input().split()]
    A = [int(i) for i in input().split()]

    if N == 1:
        print(1)
        return

    evenCount = 0
    oddCount = 0
    for Ai in A:
        if Ai % 2 == 0:
            evenCount += 1
        else:
            oddCount += 1

    if K == 0:
        if evenCount == 0 or oddCount == 0:
            print(factorial(N))
        else:
            print(0)

    elif N % 2 == 0:
        if evenCount == oddCount:
            f = factorial(N//2)
            print((2 * f * f) % modVal)
        else:
            print(0)
    else:
        if abs(evenCount - oddCount) == 1:
            f = factorial(N//2)
            print((f * f * ((N//2) + 1)) % modVal)
        else:
            print(0)

if __name__ == "__main__":
    for _ in range(int(input())):
        test()

Thanks