n = int(eval(input()))

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

mod = 10 ** 9 + 7

ans = 1

cnt = [0, 0, 0]



for i in range(n):

    ans *= cnt.count(a[i])

    if a[i] not in cnt:

        print((0))

        exit()

    ind = cnt.index(a[i])

    cnt[ind] += 1



print((ans % mod))
n = int(eval(input()))

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

mod = 10 ** 9 + 7

ans = 1

cnt = [0, 0, 0]



for i in a:

    ans *= cnt.count(i)

    ans %= mod

    if i not in cnt:

        print((0))

        exit()

    ind = cnt.index(i)

    cnt[ind] += 1



print(ans)
n = int(eval(input()))
a = list(map(int, input().split()))
mod = 10**9 + 7
ans = 1
cnt = [0, 0, 0]
for i in range(n):
    ans *= cnt.count(a[i])
    if a[i] not in cnt:
        print((0))
        exit()
    ind = cnt.index(a[i])
    cnt[ind] += 1
print((ans % mod))

n = int(eval(input()))
a = list(map(int, input().split()))
mod = 10**9 + 7
ans = 1
cnt = [0, 0, 0]
for i in a:
    ans *= cnt.count(i)
    ans %= mod
    if i not in cnt:
        print((0))
        exit()
    ind = cnt.index(i)
    cnt[ind] += 1
print(ans)

-for i in range(n): -    ans *= cnt.count(a[i]) -    if a[i] not in cnt: +for i in a: +    ans *= cnt.count(i) +    ans %= mod +    if i not in cnt: -    ind = cnt.index(a[i]) +    ind = cnt.index(i) -print((ans % mod)) +print(ans)