N = int(eval(input()))

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

mod = 10**9+7

color = [0]*3

ans = 1

for i in range(N):

  if A[i] not in color:

    ans = 0

    break

  ind = color.index(A[i])

  ans *= len([c for c in color if c == A[i]])

  ans %= mod

  color[ind] += 1



#ans = rec(color,0)

print(ans)
from collections import defaultdict

def solve():

  d = defaultdict(lambda: 0)

  N = int(eval(input()))

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

  mod = 10**9+7

  d[0] = 3

  ans = 1

  for i in range(N):

    if d[A[i]]==0:

      return 0

    ans *= d[A[i]]

    ans %= mod

    d[A[i]] -= 1

    d[A[i]+1] += 1

  return ans

print((solve()))
N = int(eval(input()))
A = list(map(int, input().split()))
mod = 10**9 + 7
color = [0] * 3
ans = 1
for i in range(N):
    if A[i] not in color:
        ans = 0
        break
    ind = color.index(A[i])
    ans *= len([c for c in color if c == A[i]])
    ans %= mod
    color[ind] += 1
# ans = rec(color,0)
print(ans)

from collections import defaultdict


def solve():
    d = defaultdict(lambda: 0)
    N = int(eval(input()))
    A = list(map(int, input().split()))
    mod = 10**9 + 7
    d[0] = 3
    ans = 1
    for i in range(N):
        if d[A[i]] == 0:
            return 0
        ans *= d[A[i]]
        ans %= mod
        d[A[i]] -= 1
        d[A[i] + 1] += 1
    return ans


print((solve()))

-N = int(eval(input())) -A = list(map(int, input().split())) -mod = 10**9 + 7 -color = [0] * 3 -ans = 1 -for i in range(N): -    if A[i] not in color: -        ans = 0 -        break -    ind = color.index(A[i]) -    ans *= len([c for c in color if c == A[i]]) -    ans %= mod -    color[ind] += 1 -# ans = rec(color,0) -print(ans) +from collections import defaultdict + + +def solve(): +    d = defaultdict(lambda: 0) +    N = int(eval(input())) +    A = list(map(int, input().split())) +    mod = 10**9 + 7 +    d[0] = 3 +    ans = 1 +    for i in range(N): +        if d[A[i]] == 0: +            return 0 +        ans *= d[A[i]] +        ans %= mod +        d[A[i]] -= 1 +        d[A[i] + 1] += 1 +    return ans + + +print((solve()))