import sys

input = sys.stdin.readline





def main():

    mod = 1000000007

    N = int(eval(input()))

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

    cnt = [[0, 0, 0] for i in range(N+1)]

    ans = 1

    for i in range(N):

        a = A[i]

        ans = ans * cnt[i].count(a) % mod

        for j in range(3):

            cnt[i+1][j] = cnt[i][j]

        for j in range(3):

            if cnt[i][j] == a:

                cnt[i+1][j] = a+1

                break

    print(ans)





if __name__ == '__main__':

    main()

import sys

input = sys.stdin.readline





def main():

    mod = 1000000007

    N = int(eval(input()))

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

    cnt = [0, 0, 0]

    ans = 1

    for a in A:

        ans = ans * cnt.count(a) % mod

        for j in range(3):

            if cnt[j] == a:

                cnt[j] = a+1

                break

    print(ans)





if __name__ == '__main__':

    main()

import sys

input = sys.stdin.readline


def main():
    mod = 1000000007
    N = int(eval(input()))
    A = list(map(int, input().split()))
    cnt = [[0, 0, 0] for i in range(N + 1)]
    ans = 1
    for i in range(N):
        a = A[i]
        ans = ans * cnt[i].count(a) % mod
        for j in range(3):
            cnt[i + 1][j] = cnt[i][j]
        for j in range(3):
            if cnt[i][j] == a:
                cnt[i + 1][j] = a + 1
                break
    print(ans)


if __name__ == "__main__":
    main()

import sys

input = sys.stdin.readline


def main():
    mod = 1000000007
    N = int(eval(input()))
    A = list(map(int, input().split()))
    cnt = [0, 0, 0]
    ans = 1
    for a in A:
        ans = ans * cnt.count(a) % mod
        for j in range(3):
            if cnt[j] == a:
                cnt[j] = a + 1
                break
    print(ans)


if __name__ == "__main__":
    main()

-    cnt = [[0, 0, 0] for i in range(N + 1)] +    cnt = [0, 0, 0] -    for i in range(N): -        a = A[i] -        ans = ans * cnt[i].count(a) % mod +    for a in A: +        ans = ans * cnt.count(a) % mod -            cnt[i + 1][j] = cnt[i][j] -        for j in range(3): -            if cnt[i][j] == a: -                cnt[i + 1][j] = a + 1 +            if cnt[j] == a: +                cnt[j] = a + 1