def main():

    MOD = 10 ** 9 + 7

    N = int(eval(input()))

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

    ans = 1

    d = [0] * (max(A) + 1)

    for a in A:

        if a == 0:

            ans *= 3 - d[a]

            ans %= MOD

            d[a] += 1

        else:

            ans *= d[a-1] - d[a]

            d[a] += 1

            ans %= MOD



    print(ans)







if __name__ == "__main__":

    main()
def main():

    N = int(eval(input()))

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

    MOD = 10 ** 9 + 7

    ans = 1

    cnt = [0] * (N + 1)

    for i in range(N):

        if A[i] == 0:

            ans *= 3 - cnt[A[i]]

            cnt[A[i]] += 1

        else:

            ans *= cnt[A[i]-1] - cnt[A[i]]

            cnt[A[i]] += 1

        ans %= MOD



    print(ans)



    







if __name__ == "__main__":

    main()
def main():
    MOD = 10**9 + 7
    N = int(eval(input()))
    A = list(map(int, input().split()))
    ans = 1
    d = [0] * (max(A) + 1)
    for a in A:
        if a == 0:
            ans *= 3 - d[a]
            ans %= MOD
            d[a] += 1
        else:
            ans *= d[a - 1] - d[a]
            d[a] += 1
            ans %= MOD
    print(ans)


if __name__ == "__main__":
    main()

def main():
    N = int(eval(input()))
    A = list(map(int, input().split()))
    MOD = 10**9 + 7
    ans = 1
    cnt = [0] * (N + 1)
    for i in range(N):
        if A[i] == 0:
            ans *= 3 - cnt[A[i]]
            cnt[A[i]] += 1
        else:
            ans *= cnt[A[i] - 1] - cnt[A[i]]
            cnt[A[i]] += 1
        ans %= MOD
    print(ans)


if __name__ == "__main__":
    main()

-    MOD = 10**9 + 7 +    MOD = 10**9 + 7 -    d = [0] * (max(A) + 1) -    for a in A: -        if a == 0: -            ans *= 3 - d[a] -            ans %= MOD -            d[a] += 1 +    cnt = [0] * (N + 1) +    for i in range(N): +        if A[i] == 0: +            ans *= 3 - cnt[A[i]] +            cnt[A[i]] += 1 -            ans *= d[a - 1] - d[a] -            d[a] += 1 -            ans %= MOD +            ans *= cnt[A[i] - 1] - cnt[A[i]] +            cnt[A[i]] += 1 +        ans %= MOD