#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<string> vs;
typedef vector<char> vc;
typedef queue<ll> ql;
typedef deque<ll> dql;
typedef priority_queue<ll> pql;
typedef set<ll> sl;
typedef pair<ll, ll> pl;
typedef map<ll, ll> ml;
typedef vector<vl> vvl;
typedef vector<pl> vpl;
#define rep(i, n) for(ll i = 0; i < ll(n); i++)
#define rep2(i, k, n) for(ll i = ll(k); i <= ll(n); i++)
#define rep3(i, n, k) for(ll i = ll(n); i >= ll(k); i--)
#define all(v) (v).begin(), (v).end()
ll mod(ll a, ll b) {return (a % b + b) % b;}
ll quo(ll a, ll b) {return (a - mod(a, b)) / b;}
template <typename T, typename U> bool chmin(T &a, const U b) {if(a > b) {a = b; return 1;} return 0;}
template <typename T, typename U> bool chmax(T &a, const U b) {if(a < b) {a = b; return 1;} return 0;}
const ll INF = 1LL << 60;
const ll MOD = 1e9 + 7;
//const ll MOD = 998244353;
const ll MAX = 2e5;
const ld eps = 1e-9;
const char newl = '\n';

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

  ll n, u, v, w, ans = 0;
  cin >> n;
  vl d(n, INF);
  vector<vpl> G(n);
  rep(_, n-1) {
    cin >> u >> v >> w;
    u--, v--;
    G[u].push_back(pl(v, w));
    G[v].push_back(pl(u, w));
  }
  ql q;
  d[0] = 0;
  q.push(0);
  while(!q.empty()) {
    ll x = q.front();
    q.pop();
    for(pl p: G[x]) {
      if(d[p.first] == INF) {
        d[p.first] = d[x] ^ p.second;
        q.push(p.first);
      }
    }
  }
  vl exp2(60), cnt(60);
  exp2[0] = 1;
  rep(j, 59) exp2[j+1] = exp2[j] * 2;
  rep(i, n) rep(j, 60) if(d[i] & exp2[j]) cnt[j]++;
  rep(j, 60) (ans += exp2[j] % MOD * cnt[j] % MOD * (n - cnt[j]) % MOD) %= MOD;
  cout << ans << newl;
  return 0; 
}
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+10;
const int mod=1e9+7;
char s[maxn];int n,vis[300],ans=0,cnt=0;
int dp[maxn][17],f[maxn][17][17];
int mp[300];
void dfs(int pos)
{
	if(pos>n)	return;
	
	for(int i=1;i<=16;i++){
		dp[pos][i]=(dp[pos][i]+1LL*i*dp[pos-1][i]%mod)%mod;
		dp[pos][i]=(dp[pos][i]+1LL*(17-i)*dp[pos-1][i-1]%mod)%mod;
	}
	if(pos!=1){
		dp[pos][1]=(dp[pos][1]+15)%mod;
	}else{
		dp[pos][1]=(dp[pos][1]+(mp[s[pos]]-1))%mod;
	}
	
	if(pos!=1){
		for(int i=0;i<mp[s[pos]];i++){
			if(vis[i]==0)
				dp[pos][cnt+1]=(dp[pos][cnt+1]+1)%mod;
			else
				dp[pos][cnt]=(dp[pos][cnt]+1)%mod;
		}
	}
	
	
	if(vis[mp[s[pos]]]==0)	cnt++,vis[mp[s[pos]]]=1;
	//cout<<pos<<" "<<dp[pos][1]<<endl;
	dfs(pos+1);
}
int main()
{
	for(int i=0;i<10;i++)	mp[i+'0']=i;
	for(int i=0;i<6;i++)	mp[i+'A']=i+10;
	scanf("%s",s+1);n=strlen(s+1);int k;scanf("%d",&k);
	dfs(1);if(cnt==k)	dp[n][k]++;
	cout<<dp[n][k]<<endl;
}