Buggy python submission (verdict: Runtime Error):
p = 10**9+7
N = int(input())
A = list(map(int,input().split()))
C={0:1}
ans = 3
for i in range(1,N):
    a = A[i]
    if a not in C:
        C[a]=0
    C[a] += 1
    if C[a]==1:
        ans = (ans*C[a-1])%p
    elif a==0 and C[a]==2:
        ans = ans*2
    elif a>0 and C[a]==2:
        ans = (ans*(C[a-1]-1))%p
print(ans)

Fixed submission:
p = 10**9+7
N = int(input())
A = list(map(int,input().split()))
cnt = 1
cnt0 =0
C = {i:0 for i in range(N)}
B = []
for i in range(N):
    a = A[i]
    if a==0:
        cnt = (cnt*(3-cnt0))%p
        cnt0 += 1
    else:
        cnt = (cnt*C[a-1])%p
        C[a-1] -= 1
    C[a] += 1
print(cnt)

Buggy python submission (verdict: Runtime Error):
p = 10**9+7
N = int(input())
A = list(map(int,input().split()))
ans = 3
C = {0:1}
for i in range(1,N):
    a = A[i]
    if a not in C:
        C[a] = 0
    C[a] += 1
    if C[a]==1:
        ans = (ans*C[a-1])%p
    elif a>0 and C[a]==2:
        ans = (ans*(C[a-1]-1))%p
    elif a==0 and C[a]==2:
        ans = (ans*2)%p
print(ans)

Fixed submission:
p = 10**9+7
N = int(input())
A = list(map(int,input().split()))
cnt = 1
cnt0 =0
C = {i:0 for i in range(N)}
B = []
for i in range(N):
    a = A[i]
    if a==0:
        cnt = (cnt*(3-cnt0))%p
        cnt0 += 1
    else:
        cnt = (cnt*C[a-1])%p
        C[a-1] -= 1
    C[a] += 1
print(cnt)

Buggy python submission (verdict: Runtime Error):
p = 10**9+7
N = int(input())
A = list(map(int,input().split()))
cnt = 1
cnt0 =0
C = {0:0}
B = []
for i in range(N):
    a = A[i]
    if a==0:
        cnt = (cnt*(3-cnt0))%p
        cnt0 += 1
    else:
        cnt = (cnt*C[a-1])%p
        C[a-1] -= 1
    if a not in C:
        C[a] = 0
    C[a] += 1
print(cnt)

Fixed submission:
p = 10**9+7
N = int(input())
A = list(map(int,input().split()))
cnt = 1
cnt0 =0
C = {i:0 for i in range(N)}
B = []
for i in range(N):
    a = A[i]
    if a==0:
        cnt = (cnt*(3-cnt0))%p
        cnt0 += 1
    else:
        cnt = (cnt*C[a-1])%p
        C[a-1] -= 1
    C[a] += 1
print(cnt)

Buggy python submission (verdict: Runtime Error):
p = 10**9+7
N = int(input())
A = list(map(int,input().split()))
C = {}
D = {}
cnt = 1
for i in range(N):
    a = A[i]
    if a not in C:
        C[a] = 0
        D[a] = 0
    C[a] += 1
    D[a] += 1
    if a>0:
        cnt = (cnt*C[a-1])%p
        C[a-1] -= 1
if D[0]==3:
    cnt = (cnt*6)%p
else:
    cnt = (cnt*3)%p
print(cnt)

Fixed submission:
p = 10**9+7
N = int(input())
A = list(map(int,input().split()))
cnt = 1
cnt0 =0
C = {i:0 for i in range(N)}
B = []
for i in range(N):
    a = A[i]
    if a==0:
        cnt = (cnt*(3-cnt0))%p
        cnt0 += 1
    else:
        cnt = (cnt*C[a-1])%p
        C[a-1] -= 1
    C[a] += 1
print(cnt)

Buggy python submission (verdict: Runtime Error):
p = 10**9+7
N = int(input())
A = list(map(int,input().split()))
C = {}
D = {}
cnt = 1
for i in range(N):
    a = A[i]
    if a not in C:
        C[a] = 0
        D[a] = 0
    C[a] += 1
    D[a] += 1
    if a>0:
        cnt = (cnt*C[a-1])%p
        C[a-1] -= 1
if D[0]==3:
    cnt = (cnt*6)%p
else:
    cnt = (cnt*3)%p
print(cnt)

Fixed submission:
p = 10**9+7
N = int(input())
A = list(map(int,input().split()))
cnt = 1
cnt0 =0
C = {i:0 for i in range(N)}
B = []
for i in range(N):
    a = A[i]
    if a==0:
        cnt = (cnt*(3-cnt0))%p
        cnt0 += 1
    else:
        cnt = (cnt*C[a-1])%p
        C[a-1] -= 1
    C[a] += 1
print(cnt)