description: There is a city park represented as a tree with n attractions as its vertices and n - 1 rails as its edges. The i-th attraction has happiness value a_i.
  
  Each rail has a color. It is either black if t_i = 0, or white if t_i = 1. Black trains only operate on a black rail track, and white trains only operate on a white rail track. If you are previously on a black train and want to ride a white train, or you are previously on a white train and want to ride a black train, you need to use 1 ticket.
  
  The path of a tour must be a simple path — it must not visit an attraction more than once. You do not need a ticket the first time you board a train. You only have k tickets, meaning you can only switch train types at most k times. In particular, you do not need a ticket to go through a path consisting of one rail color.
  
  Define f(u, v) as the sum of happiness values of the attractions in the tour (u, v), which is a simple path that starts at the u-th attraction and ends at the v-th attraction. Find the sum of f(u,v) for all valid tours (u, v) (1 ≤ u ≤ v ≤ n) that does not need more than k tickets, modulo 10^9 + 7.
  
  Input
  
  The first line contains two integers n and k (2 ≤ n ≤ 2 ⋅ 10^5, 0 ≤ k ≤ n-1) — the number of attractions in the city park and the number of tickets you have.
  
  The second line contains n integers a_1, a_2,…, a_n (0 ≤ a_i ≤ 10^9) — the happiness value of each attraction.
  
  The i-th of the next n - 1 lines contains three integers u_i, v_i, and t_i (1 ≤ u_i, v_i ≤ n, 0 ≤ t_i ≤ 1) — an edge between vertices u_i and v_i with color t_i. The given edges form a tree.
  
  Output
  
  Output an integer denoting the total happiness value for all valid tours (u, v) (1 ≤ u ≤ v ≤ n), modulo 10^9 + 7.
  
  Examples
  
  Input
  
  
  5 0
  1 3 2 6 4
  1 2 1
  1 4 0
  3 2 1
  2 5 0
  
  
  Output
  
  
  45
  
  
  Input
  
  
  3 1
  1 1 1
  1 2 1
  3 2 0
  
  
  Output
  
  
  10
solution: #include <bits/stdc++.h>
  using namespace std;
  const int N = 2e5 + 11, mod = 1e9 + 7;
  int n, k, rt, mx[N], dep, tot;
  bool vis[N];
  int a[N], sz[N], ans;
  int t[2][N], s[2][N], p[2][N], q[2][N];
  vector<pair<int, int> > to[N];
  void ins(int x, int y, int z) {
    ++x;
    for (; x <= min(tot, k) + 1; x += x & -x)
      s[z][x] = (s[z][x] + y) % mod, ++t[z][x];
  }
  int query(int x, int y, int z) {
    ++x;
    x = min(x, tot + 1);
    x = min(x, k + 1);
    int res = 0;
    for (; x; x -= x & -x)
      res = (res + 1ll * s[z][x] + 1ll * t[z][x] * y % mod) % mod;
    return res;
  }
  void dfs1(int x, int fa) {
    sz[x] = 1;
    mx[x] = 0;
    for (auto v : to[x]) {
      if (vis[v.first] || v.first == fa) continue;
      dfs1(v.first, x);
      sz[x] += sz[v.first];
      mx[x] = max(mx[x], sz[v.first]);
    }
  }
  void dfs2(int x, int fa, int y) {
    mx[x] = max(mx[x], tot - sz[x]);
    if (mx[rt] > mx[x]) rt = x;
    for (auto v : to[x]) {
      if (vis[v.first] || v.first == fa) continue;
      dfs2(v.first, x, y);
    }
  }
  void dfs3(int x, int fa, int y, int sum, int cnt, int las) {
    if (k < cnt) return;
    dep = max(dep, cnt);
    if (k >= cnt) ans = (0ll + ans + sum + query(k - cnt, sum, y)) % mod;
    if (k - 1 >= cnt) ans = (ans + query(k - 1 - cnt, sum, y ^ 1)) % mod;
    for (auto v : to[x]) {
      if (vis[v.first] || v.first == fa) continue;
      dfs3(v.first, x, y, (sum + a[v.first]) % mod, cnt + (las ^ v.second),
           v.second);
    }
  }
  void dfs4(int x, int fa, int y, int sum, int cnt, int las) {
    if (k < cnt) return;
    ins(cnt, sum, y);
    for (auto v : to[x]) {
      if (vis[v.first] || v.first == fa) continue;
      dfs4(v.first, x, y, (sum + a[v.first]) % mod, cnt + (las ^ v.second),
           v.second);
    }
  }
  void solve(int x) {
    rt = x;
    dfs1(x, x);
    tot = sz[x];
    dfs2(x, x, x);
    tot = sz[x];
    x = rt;
    vis[rt] = 1;
    for (auto v : to[x]) {
      if (vis[v.first]) continue;
      dfs3(v.first, x, v.second, (a[x] + a[v.first]) % mod, 0, v.second);
      dfs4(v.first, x, v.second, a[v.first], 0, v.second);
    }
    for (int i = (0); i <= (min(tot, k) + 1); i++)
      for (int j = (0); j <= (1); j++) s[j][i] = t[j][i] = 0;
    for (auto v : to[x]) {
      if (vis[v.first]) continue;
      solve(v.first);
    }
  }
  int main() {
    scanf("%d%d", &n, &k);
    for (int i = (1); i <= (n); i++) {
      scanf("%d", a + i);
      ans = (ans + a[i]) % mod;
    }
    for (int i = (1); i <= (n - 1); i++) {
      int x, y, z;
      scanf("%d%d%d", &x, &y, &z);
      to[x].push_back(make_pair(y, z));
      to[y].push_back(make_pair(x, z));
    }
    solve(1);
    printf("%d\n", ans);
    return 0;
  }
  