import copy

n = int(eval(input()))

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

dp = [[0,0,0] for _ in range(n+1)]

mod = 10**9+7

ans = 1

for i in range(1,n+1):

  dp[i] = copy.copy(dp[i-1])

  for j in range(3):

    if dp[i-1][j] == a[i-1]:

      dp[i][j] += 1

      break

  dp[i].sort(reverse=True)

for i in range(n):

  ans = ans*dp[i].count(a[i])%mod

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

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

mod = 10**9+7

dp = [[0]*3 for _ in range(n+1)]

ans = 1

for i in range(n):

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

    ans %= mod

    for j in range(3):

        dp[i+1][j] = dp[i][j]

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

            dp[i+1][j] += 1

            a[i] = -1

print(ans)
import copy

n = int(eval(input()))
a = list(map(int, input().split()))
dp = [[0, 0, 0] for _ in range(n + 1)]
mod = 10**9 + 7
ans = 1
for i in range(1, n + 1):
    dp[i] = copy.copy(dp[i - 1])
    for j in range(3):
        if dp[i - 1][j] == a[i - 1]:
            dp[i][j] += 1
            break
    dp[i].sort(reverse=True)
for i in range(n):
    ans = ans * dp[i].count(a[i]) % mod
print(ans)

n = int(eval(input()))
a = list(map(int, input().split()))
mod = 10**9 + 7
dp = [[0] * 3 for _ in range(n + 1)]
ans = 1
for i in range(n):
    ans *= dp[i].count(a[i])
    ans %= mod
    for j in range(3):
        dp[i + 1][j] = dp[i][j]
        if dp[i][j] == a[i]:
            dp[i + 1][j] += 1
            a[i] = -1
print(ans)

-import copy - -dp = [[0, 0, 0] for _ in range(n + 1)] +dp = [[0] * 3 for _ in range(n + 1)] -for i in range(1, n + 1): -    dp[i] = copy.copy(dp[i - 1]) +for i in range(n): +    ans *= dp[i].count(a[i]) +    ans %= mod -        if dp[i - 1][j] == a[i - 1]: -            dp[i][j] += 1 -            break -    dp[i].sort(reverse=True) -for i in range(n): -    ans = ans * dp[i].count(a[i]) % mod +        dp[i + 1][j] = dp[i][j] +        if dp[i][j] == a[i]: +            dp[i + 1][j] += 1 +            a[i] = -1