Problem:
Score : 600 points

Problem Statement

You are given N integers. The i-th integer is a_i.
Find the number, modulo 998244353, of ways to paint each of the integers red, green or blue so that the following condition is satisfied:

Let R, G and B be the sums of the integers painted red, green and blue, respectively. There exists a triangle with positive area whose sides have lengths R, G and B.

Constraints

3 \leq N \leq 300

1 \leq a_i \leq 300(1\leq i\leq N)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
a_1
:
a_N

Output

Print the number, modulo 998244353, of ways to paint each of the integers red, green or blue so that the condition is satisfied.

Sample Input 1

4
1
1
1
2

Sample Output 1

18

We can only paint the integers so that the lengths of the sides of the triangle will be 1, 2 and 2, and there are 18 such ways.

Sample Input 2

6
1
3
2
3
5
2

Sample Output 2

150

Sample Input 3

20
3
1
4
1
5
9
2
6
5
3
5
8
9
7
9
3
2
3
8
4

Sample Output 3

563038556

Solution in C++14 (GCC 5.4.1) (judge verdict: Runtime Error)

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <iomanip>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <climits>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

constexpr ll INF = 1e18;
constexpr ll MOD = 998244353;
constexpr double PI = 3.14159265358979323846;
constexpr int di[] = {0, 0, 1, -1};
constexpr int dj[] = {1, -1, 0, 0};

ll fastPow(ll x, ll n)
{
    if (n == 0)
        return 1;
    if (n % 2 == 0)
        return fastPow(x * x % MOD, n / 2);
    else
        return x * fastPow(x, n - 1) % MOD;
}

//########################
//組み合わせ
//TODO:combInit()実行
//########################
//TODO:数が大きい場合は適宜修正
ll fac[112346];
void combInit(int mx)
{
    fac[0] = 0;
    fac[1] = 1;
    for (int i = 2; i <= mx; i++)
    {
        fac[i] = fac[i - 1] * i % MOD;
    }
}

ll modDiv(ll a, ll b)
{
    return a * fastPow(b, MOD - 2) % MOD;
}

ll comb(ll a, ll b)
{
    if (b == 0)
        return 1;
    if (a < b)
        return 0;
    if (a <= 0 || b < 0)
        return 0;
    if (a == b)
        return 1;

    ll p, c;

    c = modDiv(fac[a], fac[a - b]);
    p = fac[b];

    ll res = modDiv(c, p);

    return res;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    combInit(112345);
    int n, a[310];

    int s = 0;

    cin >> n;
    REP(i, n)
    cin >> a[i], s += a[i];

    map<int, int> mp;
    V v;

    REP(i, n)
    {
        if (!mp[a[i]])
        {
            v.push_back(a[i]);
        }
        mp[a[i]]++;
    }

    sort(a, a + n);

    ll dp[2][4501][3000];

    memset(dp, 0, sizeof(dp));

    int rma = 0;
    int gma = 0;

    auto cur = dp[0];
    auto nex = dp[1];

    int sum[310];
    sum[0] = 0;
    REP(i, n)
    sum[i + 1] = sum[i] + a[i];

    REP(i, v.size())
    {
        REP(j, rma + 1)
        REP(k, gma + 1)
        nex[j][k] = 0;

        cur[0][0] = 1;
        int rman = rma;
        int gman = gma;
        for (int j = 0; j <= rma; j++)
        {
            if (j + (s - sum[i]) < s / 3)
                continue;
            for (int k = 0; k <= gma; k++)
            {
                for (int x = 1; x <= mp[v[i]]; x++)
                {
                    if (j + v[i] * x < 4500)
                    {
                        (nex[j + v[i] * x][k] += cur[j][k] * comb(mp[v[i]], x)) %= MOD;
                        rman = max(rman, j + v[i] * x);
                    }
                    if (k + v[i] * x < 3001)
                    {
                        (nex[j][k + v[i] * x] += cur[j][k] * comb(mp[v[i]], x)) %= MOD;
                        gman = max(gman, k + v[i] * x);
                    }
                    (nex[j][k] += cur[j][k]) %= MOD;
                }
            }
        }

        rma = min(rman, (s + 1) / 2);
        gma = min(gman, (s + 1) / 3);

        swap(cur, nex);
    }

    ll res = 0;

    for (int r = s / 3; r <= s / 2; r++)
    {
        for (int g = 1; g <= s / 3; g++)
        {
            int b = s - r - g;

            if (max({r, g, b}) != r)
                continue;

            if (r == g + b)
                continue;

            if (g > b)
                continue;

            //cout << r << " " << g << " " << b << endl;

            ll tmp = 0;
            (tmp += cur[r][g] * 6) %= MOD;

            if (r == g && g == b)
            {
                tmp = modDiv(tmp, 6);
            }
            else if (r == g || g == b || b == r)
            {
                tmp = modDiv(tmp, 2);
            }

            (res += tmp) %= MOD;
        }
    }

    cout << res << endl;

    return 0;
}