n = int(eval(input()))

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

mod = 10**9 + 7

ans = 1

rgb = [0,0,0]

for x in a:

    ans *= rgb.count(x)

    if rgb[0] == x: rgb[0] += 1

    elif rgb[1] == x :rgb[1] += 1

    elif rgb[2] == x :rgb[2] += 1

    ans %= mod



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

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

mod = 10 ** 9 + 7

data = [0, 0, 0]

ans = 1

for x in a:

    y = 0

    t = True

    for i in range(3):

        if data[i] == x:

            y += 1

            if t:

                t = False

                data[i] += 1

    ans *= y

    ans %= mod



print(ans)

n = int(eval(input()))
a = list(map(int, input().split()))
mod = 10**9 + 7
ans = 1
rgb = [0, 0, 0]
for x in a:
    ans *= rgb.count(x)
    if rgb[0] == x:
        rgb[0] += 1
    elif rgb[1] == x:
        rgb[1] += 1
    elif rgb[2] == x:
        rgb[2] += 1
    ans %= mod
print(ans)

n = int(eval(input()))
a = list(map(int, input().split()))
mod = 10**9 + 7
data = [0, 0, 0]
ans = 1
for x in a:
    y = 0
    t = True
    for i in range(3):
        if data[i] == x:
            y += 1
            if t:
                t = False
                data[i] += 1
    ans *= y
    ans %= mod
print(ans)

+data = [0, 0, 0] -rgb = [0, 0, 0] -    ans *= rgb.count(x) -    if rgb[0] == x: -        rgb[0] += 1 -    elif rgb[1] == x: -        rgb[1] += 1 -    elif rgb[2] == x: -        rgb[2] += 1 +    y = 0 +    t = True +    for i in range(3): +        if data[i] == x: +            y += 1 +            if t: +                t = False +                data[i] += 1 +    ans *= y