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



MOD = 10 ** 9 + 7

ctr = [0] * 3

ans = 1

for a in A:

    ans *= sum(a == v for v in ctr)

    for j in range(3):

        if ctr[j] == a:

            ctr[j] += 1

            break



print((ans % MOD))



N = int(eval(input()))

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

MOD = 10 ** 9 + 7



ctr = [0, 0, 0]

ans = 1

for a in X:

    ptn = sum(a == v for v in ctr)

    ans = ans * ptn % MOD



    if ans == 0:

        break



    for i in range(3):

        if ctr[i] == a:

            ctr[i] += 1

            break



print(ans)

N, *A = list(map(int, open(0).read().split()))
MOD = 10**9 + 7
ctr = [0] * 3
ans = 1
for a in A:
    ans *= sum(a == v for v in ctr)
    for j in range(3):
        if ctr[j] == a:
            ctr[j] += 1
            break
print((ans % MOD))

N = int(eval(input()))
X = list(map(int, input().split()))
MOD = 10**9 + 7
ctr = [0, 0, 0]
ans = 1
for a in X:
    ptn = sum(a == v for v in ctr)
    ans = ans * ptn % MOD
    if ans == 0:
        break
    for i in range(3):
        if ctr[i] == a:
            ctr[i] += 1
            break
print(ans)

-N, *A = list(map(int, open(0).read().split())) +N = int(eval(input())) +X = list(map(int, input().split())) -ctr = [0] * 3 +ctr = [0, 0, 0] -for a in A: -    ans *= sum(a == v for v in ctr) -    for j in range(3): -        if ctr[j] == a: -            ctr[j] += 1 +for a in X: +    ptn = sum(a == v for v in ctr) +    ans = ans * ptn % MOD +    if ans == 0: +        break +    for i in range(3): +        if ctr[i] == a: +            ctr[i] += 1 -print((ans % MOD)) +print(ans)