import sys

input = sys.stdin.readline



n = int(eval(input()))

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

rbg = [-1, -1, -1]

ans = 1

mod = 10 ** 9 + 7



for A in a:

    ans *= rbg.count(A - 1)

    ans %= mod

    for i in range(3):

        if rbg[i] == A - 1:

            rbg[i] = A

            break



print(ans)

import sys

read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines

sys.setrecursionlimit(10 ** 7)



n, *a = list(map(int, read().split()))

ans = 1

mod = 10 ** 9 + 7

memo = [0, 0, 0]

for check in a:

    if check in memo:

        ans *= memo.count(check)

        ans %= mod

        memo[memo.index(check)] += 1

    else:

        print((0))

        exit()

print(ans)

import sys

input = sys.stdin.readline
n = int(eval(input()))
a = list(map(int, input().split()))
rbg = [-1, -1, -1]
ans = 1
mod = 10**9 + 7
for A in a:
    ans *= rbg.count(A - 1)
    ans %= mod
    for i in range(3):
        if rbg[i] == A - 1:
            rbg[i] = A
            break
print(ans)

import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10**7)
n, *a = list(map(int, read().split()))
ans = 1
mod = 10**9 + 7
memo = [0, 0, 0]
for check in a:
    if check in memo:
        ans *= memo.count(check)
        ans %= mod
        memo[memo.index(check)] += 1
    else:
        print((0))
        exit()
print(ans)

-input = sys.stdin.readline -n = int(eval(input())) -a = list(map(int, input().split())) -rbg = [-1, -1, -1] +read = sys.stdin.buffer.read +readline = sys.stdin.buffer.readline +readlines = sys.stdin.buffer.readlines +sys.setrecursionlimit(10**7) +n, *a = list(map(int, read().split())) -for A in a: -    ans *= rbg.count(A - 1) -    ans %= mod -    for i in range(3): -        if rbg[i] == A - 1: -            rbg[i] = A -            break +memo = [0, 0, 0] +for check in a: +    if check in memo: +        ans *= memo.count(check) +        ans %= mod +        memo[memo.index(check)] += 1 +    else: +        print((0)) +        exit()