import sys

import collections





def solve():

    readline = sys.stdin.buffer.readline

    mod = 10 ** 9 + 7

    n = int(readline())

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

    d = collections.defaultdict(int)

    t = 1

    for i in range(n):

        if a[i] == 0:

            t *= 3 - d[0]

            d[a[i]] += 1

        else:

            t *= d[a[i]-1] - d[a[i]]

            d[a[i]] += 1

        t %= mod

    print(t)





if __name__ == '__main__':

    solve()

import sys

import collections





def solve():

    input = sys.stdin.readline

    mod = 10 ** 9 + 7

    n = int(input().rstrip('\n'))

    a = list(map(int, input().rstrip('\n').split()))

    d = collections.defaultdict(int)

    d[-1] = 3

    cnt = 1

    for v in a:

        cnt *= d[v-1]

        d[v] += 1

        d[v-1] -= 1

        cnt %= mod

    print(cnt)





if __name__ == '__main__':

    solve()

import sys
import collections


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10**9 + 7
    n = int(readline())
    a = list(map(int, readline().split()))
    d = collections.defaultdict(int)
    t = 1
    for i in range(n):
        if a[i] == 0:
            t *= 3 - d[0]
            d[a[i]] += 1
        else:
            t *= d[a[i] - 1] - d[a[i]]
            d[a[i]] += 1
        t %= mod
    print(t)


if __name__ == "__main__":
    solve()

import sys
import collections


def solve():
    input = sys.stdin.readline
    mod = 10**9 + 7
    n = int(input().rstrip("\n"))
    a = list(map(int, input().rstrip("\n").split()))
    d = collections.defaultdict(int)
    d[-1] = 3
    cnt = 1
    for v in a:
        cnt *= d[v - 1]
        d[v] += 1
        d[v - 1] -= 1
        cnt %= mod
    print(cnt)


if __name__ == "__main__":
    solve()

-    readline = sys.stdin.buffer.readline +    input = sys.stdin.readline -    n = int(readline()) -    a = list(map(int, readline().split())) +    n = int(input().rstrip("\n")) +    a = list(map(int, input().rstrip("\n").split())) -    t = 1 -    for i in range(n): -        if a[i] == 0: -            t *= 3 - d[0] -            d[a[i]] += 1 -        else: -            t *= d[a[i] - 1] - d[a[i]] -            d[a[i]] += 1 -        t %= mod -    print(t) +    d[-1] = 3 +    cnt = 1 +    for v in a: +        cnt *= d[v - 1] +        d[v] += 1 +        d[v - 1] -= 1 +        cnt %= mod +    print(cnt)