def main():

    import sys



    MOD = 1000000007

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

    C = [0] * (N + 1)

    C[0] = 3

    ans = 1

    for a in A:

        ans = ans * C[a]

        C[a] -= 1

        C[a + 1] += 1



    print((ans % MOD))

    return





if __name__ == '__main__':

    main()

import sys



read = sys.stdin.read

readline = sys.stdin.readline

readlines = sys.stdin.readlines

sys.setrecursionlimit(10 ** 9)

INF = 1 << 60





def main():

    MOD = 1000000007

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



    ans = 1

    C = [0, 0, 0]

    for a in A:

        try:

            ans = (ans * C.count(a)) % MOD

            C[C.index(a)] += 1

        except ValueError:

            print((0))

            return



    print(ans)

    return





if __name__ == '__main__':

    main()

def main():
    import sys

    MOD = 1000000007
    N, *A = list(map(int, sys.stdin.buffer.read().split()))
    C = [0] * (N + 1)
    C[0] = 3
    ans = 1
    for a in A:
        ans = ans * C[a]
        C[a] -= 1
        C[a + 1] += 1
    print((ans % MOD))
    return


if __name__ == "__main__":
    main()

import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10**9)
INF = 1 << 60


def main():
    MOD = 1000000007
    N, *A = list(map(int, read().split()))
    ans = 1
    C = [0, 0, 0]
    for a in A:
        try:
            ans = (ans * C.count(a)) % MOD
            C[C.index(a)] += 1
        except ValueError:
            print((0))
            return
    print(ans)
    return


if __name__ == "__main__":
    main()

+import sys + +read = sys.stdin.read +readline = sys.stdin.readline +readlines = sys.stdin.readlines +sys.setrecursionlimit(10**9) +INF = 1 << 60 + + -    import sys - -    N, *A = list(map(int, sys.stdin.buffer.read().split())) -    C = [0] * (N + 1) -    C[0] = 3 +    N, *A = list(map(int, read().split())) +    C = [0, 0, 0] -        ans = ans * C[a] -        C[a] -= 1 -        C[a + 1] += 1 -    print((ans % MOD)) +        try: +            ans = (ans * C.count(a)) % MOD +            C[C.index(a)] += 1 +        except ValueError: +            print((0)) +            return +    print(ans)