local mod = 1000000007

local n = io.read("*n")
local a = {}
for i = 1, n do
  a[i] = io.read("*n")
end
local p1, p2, p3 = 0, 0, 0
local ret = 1
for i = 1, n do
  b = a[i]
  local cnt = 0
  if p1 == b then cnt = cnt + 1 end
  if p2 == b then cnt = cnt + 1 end
  if p3 == b then cnt = cnt + 1 end
  ret = (ret * cnt) % mod
  if p1 == b then p1 = p1 + 1
  elseif p2 == b then p2 = p2 + 1
  elseif p3 == b then p3 = p3 + 1
  end
end
print(ret)
