N=int(eval(input()))

A=list(map(int,input().split()))

m=10**9+7

r=1

C=[0,0,0]

for a in A:

    try:

        i=C.index(a)

        r=r*sum([1 if c==a else 0 for c in C])%m

        C[i]+=1

    except:

        print((0))

        exit()

print(r)

n , *a_s = list(map(int,open(0).read().split()))

tmp = [3] + [0 for _ in range(n+1)]

ans = 1

for a in a_s:

  ans = ans * tmp[a] % 1000000007

  tmp[a] -= 1

  tmp[a+1] += 1

  

print(ans)
N = int(eval(input()))
A = list(map(int, input().split()))
m = 10**9 + 7
r = 1
C = [0, 0, 0]
for a in A:
    try:
        i = C.index(a)
        r = r * sum([1 if c == a else 0 for c in C]) % m
        C[i] += 1
    except:
        print((0))
        exit()
print(r)

n, *a_s = list(map(int, open(0).read().split()))
tmp = [3] + [0 for _ in range(n + 1)]
ans = 1
for a in a_s:
    ans = ans * tmp[a] % 1000000007
    tmp[a] -= 1
    tmp[a + 1] += 1
print(ans)

-N = int(eval(input())) -A = list(map(int, input().split())) -m = 10**9 + 7 -r = 1 -C = [0, 0, 0] -for a in A: -    try: -        i = C.index(a) -        r = r * sum([1 if c == a else 0 for c in C]) % m -        C[i] += 1 -    except: -        print((0)) -        exit() -print(r) +n, *a_s = list(map(int, open(0).read().split())) +tmp = [3] + [0 for _ in range(n + 1)] +ans = 1 +for a in a_s: +    ans = ans * tmp[a] % 1000000007 +    tmp[a] -= 1 +    tmp[a + 1] += 1 +print(ans)