import sys

read = sys.stdin.read



N, *A = list(map(int, read().split()))

mod = 10 ** 9 + 7



hats = [[0] * 3 for _ in range(N + 1)]

answer = 1



for i in range(N):

    answer *= hats[i].count(A[i])

    answer %= mod

    flag = True

    for j in range(3):

        if hats[i][j] == A[i] and flag:

            hats[i + 1][j] = A[i] + 1

            flag = False

        else:

            hats[i + 1][j] = hats[i][j]



print(answer)
import sys

read = sys.stdin.read



N, *A = list(map(int, read().split()))

mod = 10 ** 9 + 7



hats = [0] * 3

answer = 1



for i in range(N):

    answer *= hats.count(A[i])

    answer %= mod

    for j in range(3):

        if hats[j] == A[i]:

            hats[j] += 1

            break



print(answer)
import sys

read = sys.stdin.read
N, *A = list(map(int, read().split()))
mod = 10**9 + 7
hats = [[0] * 3 for _ in range(N + 1)]
answer = 1
for i in range(N):
    answer *= hats[i].count(A[i])
    answer %= mod
    flag = True
    for j in range(3):
        if hats[i][j] == A[i] and flag:
            hats[i + 1][j] = A[i] + 1
            flag = False
        else:
            hats[i + 1][j] = hats[i][j]
print(answer)

import sys

read = sys.stdin.read
N, *A = list(map(int, read().split()))
mod = 10**9 + 7
hats = [0] * 3
answer = 1
for i in range(N):
    answer *= hats.count(A[i])
    answer %= mod
    for j in range(3):
        if hats[j] == A[i]:
            hats[j] += 1
            break
print(answer)

-hats = [[0] * 3 for _ in range(N + 1)] +hats = [0] * 3 -    answer *= hats[i].count(A[i]) +    answer *= hats.count(A[i]) -    flag = True -        if hats[i][j] == A[i] and flag: -            hats[i + 1][j] = A[i] + 1 -            flag = False -        else: -            hats[i + 1][j] = hats[i][j] +        if hats[j] == A[i]: +            hats[j] += 1 +            break