n = int(eval(input()))

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

MOD = 10**9+7

ans = 1

s = [0] * 3



for i in range(n):

    ans *= len([0 for x in s if x == a[i]])

    if ans == 0: break

    ans %= MOD

    for j in range(3):

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

            s[j] += 1

            break



print((ans % MOD))

MOD = 10**9+7

n = int(eval(input()))

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

cnt = [0 for _ in range(n)]

ans = 1

for x in a:

    if x > 0:

        ans *= cnt[x-1] - cnt[x]

    else:

        ans *= 3 - cnt[0]

    cnt[x] += 1

    ans %= MOD

print(ans)

n = int(eval(input()))
a = list(map(int, input().split()))
MOD = 10**9 + 7
ans = 1
s = [0] * 3
for i in range(n):
    ans *= len([0 for x in s if x == a[i]])
    if ans == 0:
        break
    ans %= MOD
    for j in range(3):
        if s[j] == a[i]:
            s[j] += 1
            break
print((ans % MOD))

MOD = 10**9 + 7
n = int(eval(input()))
a = list(map(int, input().split()))
cnt = [0 for _ in range(n)]
ans = 1
for x in a:
    if x > 0:
        ans *= cnt[x - 1] - cnt[x]
    else:
        ans *= 3 - cnt[0]
    cnt[x] += 1
    ans %= MOD
print(ans)

+MOD = 10**9 + 7 -MOD = 10**9 + 7 +cnt = [0 for _ in range(n)] -s = [0] * 3 -for i in range(n): -    ans *= len([0 for x in s if x == a[i]]) -    if ans == 0: -        break +for x in a: +    if x > 0: +        ans *= cnt[x - 1] - cnt[x] +    else: +        ans *= 3 - cnt[0] +    cnt[x] += 1 -    for j in range(3): -        if s[j] == a[i]: -            s[j] += 1 -            break -print((ans % MOD)) +print(ans)