Problem p02975 (python)

Fixed submission:
n=int(input())
a=list(map(int,input().split()))
aa=set(a)
if len(aa)>3:
    print("No")
    exit()
elif a.count(0)==n:
    print("Yes")
    exit()
elif len(aa)<=1:
    print("No")
    exit()
l=[]
l.append(a[0])
for i in range(1,n):
    if a[i]!=a[0]:
        l.append(a[i])
        break
l.append(l[0]^l[1])

if n==0:
    exit()
else:
    if len(aa)==2:
        if n%3==0:
            if (a.count(l[0])==(n//3)*2 and a.count(l[1])==n//3) or (a.count(l[1])==(n//3)*2 and a.count(l[0])==n//3):
                print("Yes")
            else:
                print("No")
        elif n%3==1:
            if (a.count(l[0])==(n//3)*2+1 and a.count(l[1])==n//3) or (a.count(l[1])==(n//3)*2 and a.count(l[0])==n//3+1):
                print("Yes")
            else:
                print("No")
        elif n%3==2:
            if (a.count(l[0])==(n//3)*2+1 and a.count(l[1])==n//3+1) or (a.count(l[1])==(n//3)*2+1 and a.count(l[0])==n//3+1):
                print("Yes")
            else:
                print("No")


    else:
        if n%3==0:
            if a.count(l[0])==n//3 and a.count(l[1])==n//3 and a.count(l[2])==n//3:
                print("Yes")
            else:
                print("No")
        else:
            print("No")

Buggy submission (verdict: Runtime Error):
n=int(input())
a=list(map(int,input().split()))
aa=set(a)
if len(aa)>3:
    print("No")
    exit()
elif a.count(0)==n:
    print("Yes")
    exit()
elif len(aa)<=1:
    print("No")
    exit()
l=[]
l.append(a[0])
for i in range(1,n):
    if a[i]!=a[0]:
        l.append(a[i])
        break
l.append(l[0]^l[1])

if n==0:
    exit()
else:
    if l[0]==l[1]:
        if n%3==0:
            if a.count(l[0])==(n//3)*2 and a.count(l[2])==n//3:
                print("Yes")
            else:
                print("No")
        else:
            print("No")
    if l[0]==l[2]:
        if n%3==0:
            if a.count(l[0])==(n//3)*2 and a.count(l[1])==n//3:
                print("Yes")
            else:
                print("No")
        elif n%==1:
            if a.count(l[0])==(n//3)*2+1 and a.count(l[1])==n//3:
                print("Yes")
            else:
                print("No")
        else:
            print("No")
    if l[2]==l[1]:
        if n%3==0:
            if a.count(l[1])==(n//3)*2 and a.count(l[0])==n//3:
                print("Yes")
            else:
                print("No")
        elif n%3==2:
            if a.count(l[0])==(n//3)*2+1 and a.count(l[0])==n//3+1:
                print("Yes")
            else:
                print("No")
        else:
            print("No")
    else:
        if n%3==0:
            if a.count(l[0])==n//3 and a.count(l[1])==n//3 and a.count(l[2])==n//3:
                print("Yes")
            else:
                print("No")
        else:
            print("No")

Buggy submission (verdict: Runtime Error):
n=int(input())
a=list(map(int,input().split()))
aa=set(a)
if len(aa)>3:
    print("No")
    exit()
l=[]
l.append(a[0])
for i in range(1,n):
    if a[i]!=a[0]:
        l.append(a[i])
        break
l.append(l[0]^l[1])


if len(l)!=3:
    print("No")
else:
    if l[0]==l[1]:
        if n%3==0:
            if a.count(l[0])==(n//3)*2 and a.count(l[2])==n//3:
                print("Yes")
            else:
                print("No")
        elif n%3==1:
            if a.count(l[0])==(n//3)*2+1 and a.count(l[2])==n//3:
                print("Yes")
            else:
                print("No")
        else:
            if a.count(l[0])==(n//3)*2+2 and a.count(l[2])==n//3:
                print("Yes")
            else:
                print("No")
    if l[0]==l[2]:
        if n%3==0:
            if a.count(l[0])==(n//3)*2 and a.count(l[1])==n//3:
                print("Yes")
            else:
                print("No")
        elif n%3==1:
            if a.count(l[0])==(n//3)*2+1 and a.count(l[1])==n//3:
                print("Yes")
            else:
                print("No")
        else:
            if a.count(l[0])==(n//3)*2+1 and a.count(l[1])==n//3+1:
                print("Yes")
            else:
                print("No")
    if l[2]==l[1]:
        if n%3==0:
            if a.count(l[1])==(n//3)*2 and a.count(l[0])==n//3:
                print("Yes")
            else:
                print("No")
        elif n%3==1:
            if a.count(l[1])==(n//3)*2 and a.count(l[0])==n//3+1:
                print("Yes")
            else:
                print("No")
        else:
            if a.count(l[1])==(n//3)*2+1 and a.count(l[0])==n//3+1:
                print("Yes")
            else:
                print("No")
    else:
        if n%3==0:
            if a.count(l[0])==n//3 and a.count(l[1])==n//3 and a.count(l[2])==n//3:
                print("Yes")
            else:
                print("No")
        elif n%3==1:
            if a.count(l[0])==n//3+1 and a.count(l[1])==n//3 and a.count(l[2])==n//3:
                print("Yes")
            elif a.count(l[0])==n//3 and a.count(l[1])==n//3+1 and a.count(l[2])==n//3:
                print("Yes")
            elif a.count(l[0])==n//3 and a.count(l[1])==n//3 and a.count(l[2])==n//3+1:
                print("Yes")
            else:
                print("No")
        elif n%3==2:
            if a.count(l[0])==n//3+1 and a.count(l[1])==n//3+1 and a.count(l[2])==n//3:
                print("Yes")
            elif a.count(l[0])==n//3+1 and a.count(l[1])==n//3 and a.count(l[2])==n//3+1:
                print("Yes")
            elif a.count(l[0])==n//3 and a.count(l[1])==n//3+1 and a.count(l[2])==n//3+1:
                print("Yes")
            else:
                print("No")