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: Time Limit Exceeded)

#include<bits/stdc++.h>
#define ll long long int
#define ull unsigned long long int
#define ld long double
#define pb push_back
#define pf pop_front
#define mp make_pair
#define mod 998244353
#define fio ios_base::sync_with_stdio(false);cin.tie(NULL);
#define mat vector<vector<int>>
#define all(x) x.begin(),x.end()
#define F first
#define S second
#define fo(i,n) for(int i=0;i<n;i++)
#define inp(a) int a;cin>>a;
#define inpa(a,n)vector<int> a(n);fo(i,n)cin>>a[i];
#define printclock cerr<<"Time : "<<1000*(ld)clock()/(ld)CLOCKS_PER_SEC<<"ms\n";
using namespace std;
#define int ll


int n;
int a[300];
int dp[300][90001][2];
int su;
bool done[300][900001][2];

int f(int ind,int diff)
{
	if(diff>su)
		return 0;
	int t=((diff>=0)?0:1);
	int d=abs(diff);
	if(ind>=n)
		return ((diff==0)?1:0);
	if(done[ind][d][t])
		return dp[ind][d][t];
	done[ind][d][t]=1;
	dp[ind][d][t]=((2*f(ind+1,diff+a[ind]))%mod+f(ind+1,diff-a[ind]))%mod;
	return dp[ind][d][t];
}

int modpow(int a,int n,int m=mod)
{
	int ans=1;
	while(n>0)
	{
		if(n%2)
			ans=(ans*a)%m;
		a=(a*a)%m;
		n/=2;
	}
	return ans;
}

int g(int x,int ind)
{
	if(ind==n)
		if(x==0)
			return 1;
		else
			return 0;
	return (g(x-a[ind],ind+1)+g(x,ind+1))%mod;
}

signed main()
{
	fio
	cin>>n;
	su=0;
	fo(i,n)cin>>a[i],su+=a[i];
	//fo(i,n)fo(j,su+1)fo(k,2)dp[i][j][k]=-1;
	int ans=0;
	for(int i=0;i<=su;i++)
		ans=(ans+f(0,i))%mod;
	//cout<<ans<<endl;
	if(su%2==0)
		ans=((ans-g(su/2,0))%mod+mod)%mod;
	//cout<<ans<<endl;
	cout<<(modpow(3,n)-(3*ans)%mod+mod)%mod<<'\n';

}