n = int(eval(input()))

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

mod = 10**9 + 7

 

ans = 1

hats = [0, 0, 0]

 

for a in A:

    count = hats.count(a)

    if count == 0:

        print((0))

        exit()

 

    idx = hats.index(a)

    hats[idx] += 1

    ans *= count

    ans %= mod

 

print(ans)
import sys

input = sys.stdin.readline

mod = 10**9 + 7



def main():

    n = int(eval(input()))

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

    candidates = [0] * (n+1)

    candidates[0] = 3

    ans = 1



    for a in A:

        ans *= candidates[a]

        ans %= mod

        candidates[a] -= 1

        candidates[a+1] += 1



    print(ans)





if __name__ == "__main__":

    main()

n = int(eval(input()))
A = list(map(int, input().split()))
mod = 10**9 + 7
ans = 1
hats = [0, 0, 0]
for a in A:
    count = hats.count(a)
    if count == 0:
        print((0))
        exit()
    idx = hats.index(a)
    hats[idx] += 1
    ans *= count
    ans %= mod
print(ans)

import sys

input = sys.stdin.readline
mod = 10**9 + 7


def main():
    n = int(eval(input()))
    A = list(map(int, input().split()))
    candidates = [0] * (n + 1)
    candidates[0] = 3
    ans = 1
    for a in A:
        ans *= candidates[a]
        ans %= mod
        candidates[a] -= 1
        candidates[a + 1] += 1
    print(ans)


if __name__ == "__main__":
    main()

-n = int(eval(input())) -A = list(map(int, input().split())) +import sys + +input = sys.stdin.readline -ans = 1 -hats = [0, 0, 0] -for a in A: -    count = hats.count(a) -    if count == 0: -        print((0)) -        exit() -    idx = hats.index(a) -    hats[idx] += 1 -    ans *= count -    ans %= mod -print(ans) + + +def main(): +    n = int(eval(input())) +    A = list(map(int, input().split())) +    candidates = [0] * (n + 1) +    candidates[0] = 3 +    ans = 1 +    for a in A: +        ans *= candidates[a] +        ans %= mod +        candidates[a] -= 1 +        candidates[a + 1] += 1 +    print(ans) + + +if __name__ == "__main__": +    main()