Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
from sys import stdin
import numpy as np
# import sys
# sys.setrecursionlimit(10**4)
# import time

s_in = lambda: stdin.readline()[:-1]  # s = s_in()
d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()


MOD = 998244353

N = d_in()
# start_time = time.time()
total = 3 ** N - (2**N - 2) * 3 - 3

a_list = []
for _ in range(N):
    a_list.append(d_in())
sum_a = sum(a_list)

# 1要素目: i番目の整数までを考慮
# 2要素目: [both>0, 緑>0&青==0, 緑==0&青>0, both==0]
# 3要素目: 最大の色(赤とする)の和
dp = np.zeros((N+1, 4, 1 + (sum_a+1)//2), dtype=int)
dp[0, 3, 0] = 1

for i in range(1, N+1):
    # a = d_in()
    # sum_a += a
    # 赤に入れる
    a = a_list[i-1]
    dp[i, :, a:] += dp[i-1, :, :-a]
    dp[i, :, -1] += dp[i-1, :, -a:].sum(axis=1)

    # 緑に入れる
    dp[i, 0, :] += dp[i-1, 0, :]
    dp[i, 0, :] += dp[i-1, 2, :]
    dp[i, 1, :] += dp[i-1, 1, :]
    dp[i, 1, :] += dp[i-1, 3, :]

    # 青に入れる
    dp[i, 0, :] += dp[i-1, 0, :]
    dp[i, 0, :] += dp[i-1, 1, :]
    dp[i, 2, :] += dp[i-1, 2, :]
    dp[i, 2, :] += dp[i-1, 3, :]

    # # まとめる ---> 数ms...あまり寄与せず
    # dp[:, i, 0] = dp[:, i, 0] + dp[:, i-1, 0]*2 + dp[:, i-1, 2] + dp[:, i-1, 1]
    # dp[:, i, 1] = dp[:, i, 1] + dp[:, i-1, 1] + dp[:, i-1, 3]
    # dp[:, i, 2] = dp[:, i, 2] + dp[:, i-1, 2] + dp[:, i-1, 3]

    if i % 15 == 0:
        dp %= MOD

dp %= MOD
print((total - dp[-1, 0, -1].sum() * 3) % MOD)


Fixed submission:
# -*- coding: utf-8 -*-
from sys import stdin
import numpy as np
# import sys
# sys.setrecursionlimit(10**4)
# import time

s_in = lambda: stdin.readline()[:-1]  # s = s_in()
d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()


MOD = 998244353

N = d_in()
total = 3 ** N - (2**N - 2) * 3 - 3

a_list = []
for _ in range(N):
    a_list.append(d_in())
sum_a = sum(a_list)

# 1要素目: i番目の整数までを考慮
# 2要素目: [both>0, 緑>0&青==0, 緑==0&青>0, both==0]
# 3要素目: 最大の色(赤とする)の和
dp = np.zeros((N+1, 3, 1 + (sum_a+1)//2), dtype=int)
dp[0, 2, 0] = 1

for i in range(1, N+1):
    # 赤に入れる
    a = a_list[i-1]
    dp[i, :, a:] += dp[i-1, :, :-a]
    dp[i, :, -1] += dp[i-1, :, -a:].sum(axis=1)

    # 緑or青に入れる
    dp[i, 0, :] += (dp[i-1, 0, :] + dp[i-1, 1, :]) * 2
    dp[i, 1, :] += dp[i-1, 1, :]
    dp[i, 1, :] += dp[i-1, 2, :]

    if i % 10 == 0:
        dp[i, :, :] %= MOD

print((total - dp[-1, 0, -1] * 3) % MOD)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
from sys import stdin
import numpy as np
# import sys
# sys.setrecursionlimit(10**4)
# import time

s_in = lambda: stdin.readline()[:-1]  # s = s_in()
d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()


MOD = 998244353

N = d_in()
# start_time = time.time()
total = 3 ** N - (2**N - 2) * 3 - 3

a_list = []
for _ in range(N):
    a_list.append(d_in())
sum_a = sum(a_list)

# 1要素目: i番目の整数までを考慮
# 2要素目: [both>0, 緑>0&青==0, 緑==0&青>0, both==0]
# 3要素目: 最大の色(赤とする)の和
dp = np.zeros((N+1, 4, 1 + (sum_a+1)//2), dtype=int)
dp[0, 3, 0] = 1

for i in range(1, N+1):
    # a = d_in()
    # sum_a += a
    # 赤に入れる
    a = a_list[i-1]
    dp[i, :, a:] += dp[i-1, :, :-a]
    dp[i, :, -1] += dp[i-1, :, -a:].sum(axis=1)

    # 緑に入れる
    dp[i, 0, :] += dp[i-1, 0, :]
    dp[i, 0, :] += dp[i-1, 2, :]
    dp[i, 1, :] += dp[i-1, 1, :]
    dp[i, 1, :] += dp[i-1, 3, :]

    # 青に入れる
    dp[i, 0, :] += dp[i-1, 0, :]
    dp[i, 0, :] += dp[i-1, 1, :]
    dp[i, 2, :] += dp[i-1, 2, :]
    dp[i, 2, :] += dp[i-1, 3, :]

    # # まとめる ---> 数ms...あまり寄与せず
    # dp[:, i, 0] = dp[:, i, 0] + dp[:, i-1, 0]*2 + dp[:, i-1, 2] + dp[:, i-1, 1]
    # dp[:, i, 1] = dp[:, i, 1] + dp[:, i-1, 1] + dp[:, i-1, 3]
    # dp[:, i, 2] = dp[:, i, 2] + dp[:, i-1, 2] + dp[:, i-1, 3]

    if i % 10 == 0:
        dp %= MOD

dp %= MOD
# print((total - dp[-1, 0, (sum_a+1)//2:].sum() * 3) % MOD)
print((total - dp[-1, 0, -1].sum() * 3) % MOD)


Fixed submission:
# -*- coding: utf-8 -*-
from sys import stdin
import numpy as np
# import sys
# sys.setrecursionlimit(10**4)
# import time

s_in = lambda: stdin.readline()[:-1]  # s = s_in()
d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()


MOD = 998244353

N = d_in()
total = 3 ** N - (2**N - 2) * 3 - 3

a_list = []
for _ in range(N):
    a_list.append(d_in())
sum_a = sum(a_list)

# 1要素目: i番目の整数までを考慮
# 2要素目: [both>0, 緑>0&青==0, 緑==0&青>0, both==0]
# 3要素目: 最大の色(赤とする)の和
dp = np.zeros((N+1, 3, 1 + (sum_a+1)//2), dtype=int)
dp[0, 2, 0] = 1

for i in range(1, N+1):
    # 赤に入れる
    a = a_list[i-1]
    dp[i, :, a:] += dp[i-1, :, :-a]
    dp[i, :, -1] += dp[i-1, :, -a:].sum(axis=1)

    # 緑or青に入れる
    dp[i, 0, :] += (dp[i-1, 0, :] + dp[i-1, 1, :]) * 2
    dp[i, 1, :] += dp[i-1, 1, :]
    dp[i, 1, :] += dp[i-1, 2, :]

    if i % 10 == 0:
        dp[i, :, :] %= MOD

print((total - dp[-1, 0, -1] * 3) % MOD)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
from sys import stdin
import numpy as np
# import sys
# sys.setrecursionlimit(10**4)
# import time

s_in = lambda: stdin.readline()[:-1]  # s = s_in()
d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()


MOD = 998244353

N = d_in()
# start_time = time.time()
total = 3 ** N - (2**N - 2) * 3 - 3

# 1要素目: 最大の色(赤とする)の和
# 2要素目: i番目の整数までを考慮
# 3要素目: [both>0, 緑>0&青==0, 緑==0&青>0, both==0]
dp = np.zeros(300*(N+1)**2*4, dtype=int).reshape(-1, N+1, 4)
dp[0, 0, 3] = 1

sum_a = 0
for i in range(1, N+1):
    a = d_in()
    sum_a += a
    # 赤に入れる
    dp[a:, i, :] += dp[:-a, i-1, :]

    # 緑に入れる
    dp[:, i, 0] += dp[:, i-1, 0]
    dp[:, i, 0] += dp[:, i-1, 2]
    dp[:, i, 1] += dp[:, i-1, 1]
    dp[:, i, 1] += dp[:, i-1, 3]

    # 青に入れる
    dp[:, i, 0] += dp[:, i-1, 0]
    dp[:, i, 0] += dp[:, i-1, 1]
    dp[:, i, 2] += dp[:, i-1, 2]
    dp[:, i, 2] += dp[:, i-1, 3]
    if i % 10 == 0:
        dp %= MOD


print((total - dp[(sum_a+1)//2:, -1, 0].sum() * 3) % MOD)


Fixed submission:
# -*- coding: utf-8 -*-
from sys import stdin
import numpy as np
# import sys
# sys.setrecursionlimit(10**4)
# import time

s_in = lambda: stdin.readline()[:-1]  # s = s_in()
d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()


MOD = 998244353

N = d_in()
total = 3 ** N - (2**N - 2) * 3 - 3

a_list = []
for _ in range(N):
    a_list.append(d_in())
sum_a = sum(a_list)

# 1要素目: i番目の整数までを考慮
# 2要素目: [both>0, 緑>0&青==0, 緑==0&青>0, both==0]
# 3要素目: 最大の色(赤とする)の和
dp = np.zeros((N+1, 3, 1 + (sum_a+1)//2), dtype=int)
dp[0, 2, 0] = 1

for i in range(1, N+1):
    # 赤に入れる
    a = a_list[i-1]
    dp[i, :, a:] += dp[i-1, :, :-a]
    dp[i, :, -1] += dp[i-1, :, -a:].sum(axis=1)

    # 緑or青に入れる
    dp[i, 0, :] += (dp[i-1, 0, :] + dp[i-1, 1, :]) * 2
    dp[i, 1, :] += dp[i-1, 1, :]
    dp[i, 1, :] += dp[i-1, 2, :]

    if i % 10 == 0:
        dp[i, :, :] %= MOD

print((total - dp[-1, 0, -1] * 3) % MOD)

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-
from sys import stdin
import numpy as np
# import sys
# sys.setrecursionlimit(10**4)

s_in = lambda: stdin.readline()[:-1]  # s = s_in()
d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()


MOD = 998244353

N = d_in()
total = 3 ** N - (2**N - 2) * 3 - 3

# 1要素目: 最大の色(赤とする)の和
# 2要素目: i番目の整数までを考慮
# 3要素目: [both>0, 緑>0&青==0, 緑==0&青>0, both==0]
dp = np.zeros(300*(N+1)**2*4, dtype=int).reshape(-1, N+1, 4)
dp[0, 0, 3] = 1

sum_a = 0
for i in range(1, N+1):
    a = d_in()
    sum_a += a
    # 赤に入れる
    dp[a:, i, :] += dp[:-a, i-1, :]

    # 緑に入れる
    dp[:, i, 0] += dp[:, i-1, 0]
    dp[:, i, 0] += dp[:, i-1, 2]
    dp[:, i, 1] += dp[:, i-1, 1]
    dp[:, i, 1] += dp[:, i-1, 3]

    # 青に入れる
    dp[:, i, 0] += dp[:, i-1, 0]
    dp[:, i, 0] += dp[:, i-1, 1]
    dp[:, i, 2] += dp[:, i-1, 2]
    dp[:, i, 2] += dp[:, i-1, 3]
    dp %= MOD


print((total - dp[(sum_a+1)//2:, -1, 0].sum() * 3) % MOD)


Fixed submission:
# -*- coding: utf-8 -*-
from sys import stdin
import numpy as np
# import sys
# sys.setrecursionlimit(10**4)
# import time

s_in = lambda: stdin.readline()[:-1]  # s = s_in()
d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()


MOD = 998244353

N = d_in()
total = 3 ** N - (2**N - 2) * 3 - 3

a_list = []
for _ in range(N):
    a_list.append(d_in())
sum_a = sum(a_list)

# 1要素目: i番目の整数までを考慮
# 2要素目: [both>0, 緑>0&青==0, 緑==0&青>0, both==0]
# 3要素目: 最大の色(赤とする)の和
dp = np.zeros((N+1, 3, 1 + (sum_a+1)//2), dtype=int)
dp[0, 2, 0] = 1

for i in range(1, N+1):
    # 赤に入れる
    a = a_list[i-1]
    dp[i, :, a:] += dp[i-1, :, :-a]
    dp[i, :, -1] += dp[i-1, :, -a:].sum(axis=1)

    # 緑or青に入れる
    dp[i, 0, :] += (dp[i-1, 0, :] + dp[i-1, 1, :]) * 2
    dp[i, 1, :] += dp[i-1, 1, :]
    dp[i, 1, :] += dp[i-1, 2, :]

    if i % 10 == 0:
        dp[i, :, :] %= MOD

print((total - dp[-1, 0, -1] * 3) % MOD)