#include <bits/stdc++.h>
#define DB double
#define LL long long

#define MST(a,b) memset((a),(b),sizeof(a))

#ifdef _DEBUG_
#define MRK() cout<<"Mark"<<endl;
#define WRT(x) cout<<#x<<" = "<<(x)<<endl;
#else
#define MRK() ;
#define WRT(x) ;
#endif

#define MAXN 1010000
#define MAXM 2010000
#define MOD 998244353  //1000000007
#define INF 0x3f3f3f3f
#define LLINF 0x3f3f3f3f3f3f3f3f
#define EPS 1e-5

#define _ 0
using namespace std;

int n;
int w[MAXN];
LL pool[101][21000];
LL* dp[101];
LL fac[MAXN]; 
void init()
{
	for (int i=0;i<=100;i++)
		dp[i]=pool[i]+10050;
}

void work()
{
	dp[0][0]=1;
	int sum=0;
	cin>>n;
	for (int i=1;i<=n;i++)
	{
		cin>>w[i];
		sum+=w[i];
	}
	if (sum%2==1)
	{
		cout<<0<<endl;
		return ;
	}
	dp[0][0]=1;
	for (int i=1;i<=n;i++)
	{
		for (int j=i-1;j>=0;j--)
			for (int k=10000;k>=0;k--)
			{
				//dp[j+1][k]=(dp[j+1][k]+dp[j][k])%MOD;
				dp[j+1][k+w[i]]=(dp[j+1][k+w[i]]+dp[j][k])%MOD;
			}
	}
	fac[0]=1;
	for (int i=1;i<MAXN;i++)
		fac[i]=fac[i-1]*i%MOD;
	LL ans=0;
	for (int i=0;i<=n;i++)
	{
		WRT(dp[i][sum/2])
		ans+=dp[i][sum/2]*fac[i]%MOD*fac[n-i]%MOD;
	}
	cout<<ans%MOD<<endl; 
}

int main()
{
	#ifdef _DEBUG_
	freopen("C:/Users/DELL/Documents/Dev-C++/sample.in","r",stdin);
	#endif
	init();
	int casenum=1;
	//scanf("%d",&casenum);
	for (int testcase=1;testcase<=casenum;testcase++)
	{
		#ifdef _DEBUG_
		printf("Case #%d:\n",testcase);
		#endif
		work();
	}
	return ~~(0^_^0);
}





#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); ++i)
#define chmax(a, b) a = max(a, b)
#define chmin(a, b) a = min(a, b)
#define fi first
#define se second
#define INF 1000000009
#define LLINF 1000000000000000009LL
using ll = long long;

const int mod = 1000000007; // 998244353;
struct mint {
  long long x;
  mint(long long x=0) : x((x%mod+mod)%mod) {}
  mint operator-() const { return mint(-x); }
  mint& operator+=(const mint& a) {
    if ((x += a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator-=(const mint& a) {
    if ((x += mod-a.x) >= mod) x -= mod;
    return *this;
  }
  mint& operator*=(const mint& a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint operator+(const mint& a) const {
    return mint(*this) += a;
  }
  mint operator-(const mint& a) const {
    return mint(*this) -= a;
  }
  mint operator*(const mint& a) const {
    return mint(*this) *= a;
  }
  mint pow(long long t) const {
    if (!t) return 1;
    mint a = pow(t>>1);
    a *= a;
    if (t&1) a *= *this;
    return a;
  }
  // for prime mod
  mint inv() const { return pow(mod-2); }
  mint& operator/=(const mint& a) { return *this *= a.inv(); }
  mint operator/(const mint& a) const {
    return mint(*this) /= a;
  }
};
istream& operator>>(istream& is, const mint& a) {
  return is >> a.x;
}
ostream& operator<<(ostream& os, const mint& a) {
  return os << a.x;
}

typedef pair<int,ll>P;

vector<P> to[200009];
mint odd[200009], even[200009];

mint dfs(int p, int last, int n){
  mint odds=0, res=0;
  rep(i,to[p].size()){
    int next=to[p][i].fi;
    int w=(to[p][i].se>>n)&1;
    if(next==last)continue;
    res+=dfs(next,p,n);
    if(w==0){
      odd[p]+=odd[next];
      even[p]+=even[next];
    }
    else{
      odd[p]+=even[next];
      even[p]+=odd[next];
    }
  }
  rep(i,to[p].size()){
    int next=to[p][i].fi;
    int w=(to[p][i].se>>n)&1;
    if(next==last)continue;
    if(w==0){
      res += (odd[p] - odd[next]) * even[next];
    }
    else{
      res += (odd[p] - even[next]) * odd[next];
    }
  }
  res+=odd[p];
  //cout<<p<<' '<<res<<' '<<odd[p]<<endl;
  return res;
}

int main(){
  int n;
  cin>>n;
  rep(i,n-1){
    int u,v;
    ll w;
    cin>>u>>v>>w;
    u--;v--;
    to[u].push_back(P(v, w));
    to[v].push_back(P(u, w));
  }
  mint ans=0;
  rep(i,60){
    rep(j,n){
      odd[j]=0;
      even[j]=1;
    }
    ans += dfs(0,0,i)*((ll)1<<i);
  }
  cout<<ans<<endl;
  return 0;
}