Problem p02761 (python)

Fixed submission:
n,m=map(int,input().split())
s=[list(map(int,input().split())) for _ in range(m)]

l1=[]
l2=[]
l3=[]
for i in range(m):
    if s[i][0]==1:
        l1.append(s[i])
    elif s[i][0]==2:
        l2.append(s[i])
    elif s[i][0]==3:
        l3.append(s[i])
l1=list(map(list, set(map(tuple, l1))))
l2=list(map(list, set(map(tuple, l2))))
l3=list(map(list, set(map(tuple, l3))))

check=0
if len(l1)>1 or len(l2)>1 or len(l3)>1:#同じ桁に違う数
    check+=1
if n==3 or n==2:
    if len(l1)>0 and l1[0][1]==0:#二桁三桁の時最初が０
        check+=1

if n==3:
    if len(l1)==0:
        l1.append([1,1])
    if len(l2)==0:
        l2.append([2,0])
    if len(l3)==0:
        l3.append([3,0])
if n==2:
    if len(l1)==0:
        l1.append([1,1])
    if len(l2)==0:
        l2.append([2,0])
if n==1:
    if len(l1)==0:
        l1.append([1,0])

if check>=1:
    print(-1)
else:
    ans=[]
    if len(l1)>0:
        ans.append(l1[0][1])
    if len(l2)>0:
        ans.append(l2[0][1])
    if len(l3)>0:
        ans.append(l3[0][1])
    print(int("".join(map(str,ans))))

Buggy submission (verdict: Runtime Error):
n,m=map(int,input().split())
s=[list(map(int,input().split())) for _ in range(m)]

l1=[]
l2=[]
l3=[]
for i in range(m):
    if s[i][0]==1:
        l1.append(s[i])
    elif s[i][0]==2:
        l2.append(s[i])
    elif s[i][0]==3:
        l3.append(s[i])
l1=list(map(list, set(map(tuple, l1))))
l2=list(map(list, set(map(tuple, l2))))
l3=list(map(list, set(map(tuple, l3))))

check=0
if len(l1)>1 or len(l2)>1 or len(l3)>1:#同じ桁に違う数
    check+=1
if n==3 or n==2:
    if len(l1)>0 and l1[0][1]==0:#二桁三桁の時最初が０
        check+=1

if n==3:
    if len(l1)==0:
        l1.append([1,1])
    if len(l2)==0:
        l2.append([2,0])
    if len(l3)==0:
        l3.append([3,0])
if n==2:
    if len(l1)==0:
        l1.append([1,1])
    if len(l2)==0:
        l2.append([2,0])
if n==1:
    if len(l1)==0:
        l1.append([1,0])

if check>=1:
    print(-1)
else:
    ans=[]
    ans.append(l1[0][1])
    ans.append(l2[0][1])
    ans.append(l3[0][1])
    print(int("".join(map(str,ans))))

Buggy submission (verdict: Runtime Error):
n,m=map(int,input().split())
s=[list(map(int,input().split())) for _ in range(m)]

l1=[]
l2=[]
l3=[]
for i in range(m):
    if s[i][0]==1:
        l1.append(s[i])
    elif s[i][0]==2:
        l2.append(s[i])
    elif s[i][0]==3:
        l3.append(s[i])
l1=list(map(list, set(map(tuple, l1))))
l2=list(map(list, set(map(tuple, l2))))
l3=list(map(list, set(map(tuple, l3))))

check=0
if len(l1)>1 or len(l2)>1 or len(l3)>1:
    check+=1
if n==2 or n==3:   
    if len(l1)==0 and len(l2)==0 and len(l3)==0:
        check+=1
 
if n==2:
    if len(l1)==0 or l1[0][1]==0:

        check+=1
if n==3:
    if len(l1)==0 or l1[0][1]==0:
 
        check+=1

if check>=1:
    print(-1)
else:
    ans=[]
    if n==3:
        ans.append(l1[0][1])
        if len(l2)>0:
            ans.append(l2[0][1])
        else:
            ans.append(0)
        if len(l3)>0:
            ans.append(l3[0][1])
        else:
            ans.append(0)
    
    if n==2:
        ans.append(l1[0][1])
        if len(l2)>0:
            ans.append(l2[0][1])
        else:
            ans.append(0)
    
    if n==1:
        ans.append(l1[0][1])
        
    print(int("".join(map(str,ans))))

Buggy submission (verdict: Runtime Error):
n,m=map(int,input().split())
s=[list(map(int,input().split())) for _ in range(m)]

l1=[]
l2=[]
l3=[]
for i in range(m):
    if s[i][0]==1:
        l1.append(s[i])
    elif s[i][0]==2:
        l2.append(s[i])
    elif s[i][0]==3:
        l3.append(s[i])
l1=list(map(list, set(map(tuple, l1))))
l2=list(map(list, set(map(tuple, l2))))
l3=list(map(list, set(map(tuple, l3))))

check=0
if len(l1)>1 or len(l2)>1 or len(l3)>1:
    check+=1
    print(-1)
if len(l1)==0 and len(l2)==0 and len(l3)==0:
    check+=1
    print(-1)
if check==0:
    ans=[]
    if len(l1)>0:
        ans.append(l1[0][1])
    if len(l2)>0:
        ans.append(l2[0][1])
    else:
        ans.append(0)
    if len(l3)>0:
        ans.append(l3[0][1])
    else:
        ans.append(0)
    if n==2:
        ans=[ans[0],ans[1]]
    if n==1:
        ans=[ans[0]]
    if ans[0]==0:
        del ans[0]
    print(int("".join(map(str,ans))))