N = int(eval(input()))

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

MOD = 1_000_000_007



cnt = [0, 0, 0]

ans = 1

for i in range(N):

    ans *= sum(c == A[i] for c in cnt)

    ans %= MOD

    for j in range(3):

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

            cnt[j] += 1

            break

print(ans)

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

MOD = 1_000_000_007

cnt = [0, 0, 0]

ans = 1

for a in A:

    possible = sum(c == a for c in cnt)

    ans *= possible

    ans %= MOD

    for i in range(3):

        if cnt[i] == a:

            cnt[i] += 1

            break

print(ans)

N = int(eval(input()))
A = list(map(int, input().split()))
MOD = 1_000_000_007
cnt = [0, 0, 0]
ans = 1
for i in range(N):
    ans *= sum(c == A[i] for c in cnt)
    ans %= MOD
    for j in range(3):
        if cnt[j] == A[i]:
            cnt[j] += 1
            break
print(ans)

N, *A = list(map(int, open(0).read().split()))
MOD = 1_000_000_007
cnt = [0, 0, 0]
ans = 1
for a in A:
    possible = sum(c == a for c in cnt)
    ans *= possible
    ans %= MOD
    for i in range(3):
        if cnt[i] == a:
            cnt[i] += 1
            break
print(ans)

-N = int(eval(input())) -A = list(map(int, input().split())) +N, *A = list(map(int, open(0).read().split())) -for i in range(N): -    ans *= sum(c == A[i] for c in cnt) +for a in A: +    possible = sum(c == a for c in cnt) +    ans *= possible -    for j in range(3): -        if cnt[j] == A[i]: -            cnt[j] += 1 +    for i in range(3): +        if cnt[i] == a: +            cnt[i] += 1