n = gets.to_i

arr = []
n.times do |i|
  arr << gets.to_i
end

rgb_arr = Array.new(n, 0)

def f(rgb_arr, i)
  if rgb_arr[i] == nil then
   return
  end
  rgb_arr[i] += 1
  if rgb_arr[i] == 3 then
    rgb_arr[i] = 0
    f(rgb_arr, i+1)
  end
end

def g(arr, rgb_arr)
  r = 0
  g = 0
  b = 0

  arr.each_with_index do |x, i|
    if rgb_arr[i] == 0 then
      r += x
    elsif rgb_arr[i] == 1 then
      g += x
    else
      b += x
    end
  end

  return r < g + b && g < r + b && b < r + g
end

count = 0
while rgb_arr.any?{|i| i != 2} do
  if g(arr, rgb_arr) then
    count += 1
  end
  f(rgb_arr, 0)
end

puts count % 998244353



