def main():

  from collections import defaultdict

  N = int(eval(input()))

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

  MOD = 1000000007

  c = defaultdict(int)

  c[0] = 3

  ans = 1

  for i in range(N):

    if not c[A[i]]:

      print((0))

      exit()

    ans *= c[A[i]]

    ans %= MOD

    c[A[i]] -= 1

    c[A[i]+1] += 1

  print(ans)

main()  

           
def main():

  N = int(eval(input()))

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

  MOD = 1000000007

  c = [0] * (N+1)

  c[0] = 3

  ans = 1

  for i in range(N):

    if not c[A[i]]:

      print((0))

      exit()

    ans *= c[A[i]]

    ans %= MOD

    c[A[i]] -= 1

    c[A[i]+1] += 1

  print(ans)

main()  

           
def main():
    from collections import defaultdict

    N = int(eval(input()))
    A = list(map(int, input().split()))
    MOD = 1000000007
    c = defaultdict(int)
    c[0] = 3
    ans = 1
    for i in range(N):
        if not c[A[i]]:
            print((0))
            exit()
        ans *= c[A[i]]
        ans %= MOD
        c[A[i]] -= 1
        c[A[i] + 1] += 1
    print(ans)


main()

def main():
    N = int(eval(input()))
    A = list(map(int, input().split()))
    MOD = 1000000007
    c = [0] * (N + 1)
    c[0] = 3
    ans = 1
    for i in range(N):
        if not c[A[i]]:
            print((0))
            exit()
        ans *= c[A[i]]
        ans %= MOD
        c[A[i]] -= 1
        c[A[i] + 1] += 1
    print(ans)


main()

-    from collections import defaultdict - -    c = defaultdict(int) +    c = [0] * (N + 1)