from collections import defaultdict

N = int(eval(input()))



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



ans = 1

d = defaultdict(int)



for i,n in enumerate(A):

    if n == 0:

        ans *= (3-d[n])

        d[n] += 1

    else:

        ans *= (d[n-1] - d[n])

        d[n] += 1



print((ans % (10**9+7)))






N = int(eval(input()))

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

MOD = 10**9 + 7



if A[0] != 0:

    print((0))

    exit()





ans = 1

colors = [0] * 3

for n in A:

    ans *= colors.count(n)

    ans %= MOD

    for i in range(3):

        if colors[i] == n:

            colors[i] += 1

            break

print(ans)

from collections import defaultdict

N = int(eval(input()))
A = list(map(int, input().split()))
ans = 1
d = defaultdict(int)
for i, n in enumerate(A):
    if n == 0:
        ans *= 3 - d[n]
        d[n] += 1
    else:
        ans *= d[n - 1] - d[n]
        d[n] += 1
print((ans % (10**9 + 7)))

N = int(eval(input()))
A = list(map(int, input().split()))
MOD = 10**9 + 7
if A[0] != 0:
    print((0))
    exit()
ans = 1
colors = [0] * 3
for n in A:
    ans *= colors.count(n)
    ans %= MOD
    for i in range(3):
        if colors[i] == n:
            colors[i] += 1
            break
print(ans)

-from collections import defaultdict - +MOD = 10**9 + 7 +if A[0] != 0: +    print((0)) +    exit() -d = defaultdict(int) -for i, n in enumerate(A): -    if n == 0: -        ans *= 3 - d[n] -        d[n] += 1 -    else: -        ans *= d[n - 1] - d[n] -        d[n] += 1 -print((ans % (10**9 + 7))) +colors = [0] * 3 +for n in A: +    ans *= colors.count(n) +    ans %= MOD +    for i in range(3): +        if colors[i] == n: +            colors[i] += 1 +            break +print(ans)