#include<bits/stdc++.h>
#define int long long
using namespace std;
constexpr int mod=1000000007;
struct edge{int to,cost;};
void dfs(int v,int p,auto&G,auto&x){
  for(edge e:G[v])if(e.to!=p){
    x[e.to]=x[v]^e.cost;
    dfs(e.to,v,G,x);
  }
}
int mod_pow(int a,int b){
  if(!b)return 1;
  if(b&1)return mod_pow(a,b-1)*a%mod;
  int c=mod_pow(a,b/2);
  return c*c%mod;
}
signed main(){
  int N; cin>>N;
  vector<vector<edge>> G(N);
  for(int i=0;i<N-1;i++){
    int a,b,c; cin>>a>>b>>c;
    G[--a].push_back({--b,c});
    G[b].push_back({a,c});
  }
  vector<int>x(N);
  dfs(0,-1,G,x);
  int ans=0;
  for(int i=0;i<60;i++){
    int sum=0;
    for(int j:x)if((j>>i)&1)sum++;
    (ans+=(sum*(N-sum))%mod*mod_pow(2,i)%mod)%=mod;
  }
  cout<<ans<<endl;
}
#include<bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a); i<=(b); ++i)
#define ROF(i,a,b) for(int i=(a); i>=(b); --i)
#define pb push_back
#define eb emplace_back
#define SZ(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()
#define make_unique(a) sort(all((a))), (a).erase(unique(all((a))),(a).end())
#define x first
#define y second
#define MP make_pair
#define MT make_tuple
#define debug(x) cout << #x << " = " << x << endl
#define debug2(x,y) FOR(i,1,y) cout << "##"; cout << #x << " = " << x << endl
#define mset(x,y) memset((x), (y), sizeof(x))

using namespace std;

typedef long long i64;
typedef long double ld;
typedef tuple<int,int,int> iii;
typedef pair<int,int> pii;
typedef pair<i64,i64> pll;
typedef vector<int> vi;
typedef vector<i64> vl;
typedef vector<vector<int>> vvi;
typedef vector<vector<i64>> vvl;
typedef vector<pair<int,int>> vpii;
typedef vector<pair<i64,i64>> vpll;

int readInt(){ int a; scanf("%d",&a); return a; }
i64 readLong(){ i64 a; scanf("%lld",&a); return a;}
void readString(char *s){ scanf(" %s",s); }

const int mod = 998244353;
int add(int a, int b){ return ((a+=b)>=mod)?a-mod:a; }
int mul(int a, int b){ return a*1ll*b%mod; }
void adding(int &a, int b){ a = add(a, b); }
int pw(int a, int b){
	if(a==0) return 0;
	int ans = 1, res = a;
	for(int i = 1; i <= b; i<<=1, res=mul(res,res)){
		if(i&b) ans = mul(ans,res);
	}
	return ans;
}

int play(vi w){
	int n = SZ(w);
	vi p(n);
	iota(all(p),0);
	int cnt = 0;
	do{
		int sum = 0;
		FOR(i,0,n-1){
			if(sum <= 0) sum += w[p[i]];
			else sum -= w[p[i]];
		}
		if(sum == 0) ++cnt;
		else{
			printf("{ ");
			FOR(i,0,n-1) printf("%d ",w[p[i]]);
			printf("}\n");
		}
	}while(next_permutation(all(p)));
	return cnt;
}

const int N = 105;
int fac[N];
const int zero = 5000;
int dp[N][N*N];
int sav[N][N*N];

int main(){
	//printf("%d\n",play({1,2,3,4}));
	fac[0] = 1;
	FOR(i, 1, 100) fac[i] = mul(fac[i-1],i);
	int n = readInt();

	dp[0][zero] = 1;

	int mx = N*N;
	FOR(i, 1, n){
		int a = readInt();
		FOR(j, 0, i-1){
			FOR(k, 0, mx-1){
				if(k >= a) adding(sav[j][k-a], dp[j][k]);
				if(k+a < mx) adding(sav[j+1][k+a], dp[j][k]);
			}
		}
		FOR(j, 0, i){
			FOR(k, 0, mx-1){
				dp[j][k] = sav[j][k];
				sav[j][k] = 0;
			}
		}
	}
	int ans = 0;
	FOR(i, 1, n-1){
		//printf("%d\n",dp[i][zero]);
		adding(ans, mul(mul(fac[i],fac[n-i]),dp[i][zero]));
	}
	printf("%d",ans);
	return 0;
}
/*
5
3 3 3 3 6
(96)


*/
