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;
  mt19937 gen(time(0));
  int rdint(int l, int r) { return uniform_int_distribution<int>(l, r)(gen); }
  const int N = 2e5 + 5, mod = 1e9 + 7;
  int n, k, sum, rt, a[N], si[N], f[N];
  bool vis[N];
  vector<pair<int, int>> e[N];
  void getrt(int u, int p) {
    si[u] = 1;
    f[u] = 0;
    for (pair<int, int> &x : e[u]) {
      int v = x.first;
      if (v == p || vis[v]) continue;
      getrt(v, u);
      si[u] += si[v];
      f[u] = max(f[u], si[v]);
    }
    f[u] = max(f[u], sum - si[u]);
    if (f[u] < f[rt]) rt = u;
  }
  long long ans;
  int bit[2][N], sz[2][N];
  void add(int c, int x, long long v, int dv) {
    x++;
    while (x <= n)
      sz[c][x] = (sz[c][x] + dv) % mod, bit[c][x] = (bit[c][x] + v) % mod,
      x += x & -x;
  }
  long long query(int c, int x) {
    x++;
    long long ans = 0;
    while (x) ans = (ans + bit[c][x]) % mod, x -= x & -x;
    return ans;
  }
  long long querysz(int c, int x) {
    x++;
    long long ans = 0;
    while (x) ans = (ans + sz[c][x]) % mod, x -= x & -x;
    return ans;
  }
  int ad;
  void dfs(int u, int p, int c, int las, int d, long long dis) {
    if (d <= k) {
      ans = (ans + dis + ad) % mod;
      ans = (ans + query(c, k - d) + querysz(c, k - d) * dis % mod) % mod;
      if (d < k)
        ans = (ans + query(c ^ 1, k - d - 1) +
               querysz(c ^ 1, k - d - 1) * dis % mod) %
              mod;
    }
    for (pair<int, int> &x : e[u]) {
      int v = x.first;
      if (vis[v] || v == p) continue;
      dfs(v, u, c, x.second, d + (las != x.second), (dis + a[v]) % mod);
    }
  }
  void gfs(int u, int p, int c, int las, int d, long long dis, int op) {
    if (op == 1)
      add(c, d, dis % mod, 1);
    else
      add(c, d, (mod - dis) % mod, mod - 1);
    for (pair<int, int> &x : e[u]) {
      int v = x.first;
      if (vis[v] || v == p) continue;
      gfs(v, u, c, x.second, d + (las != x.second), (dis + a[v]) % mod, op);
    }
  }
  void sol(int u) {
    vis[u] = true;
    ans = (ans + a[u]) % mod;
    for (pair<int, int> &x : e[u]) {
      int v = x.first;
      if (vis[v]) continue;
      ad = a[u];
      dfs(v, u, x.second, x.second, 0, a[v]);
      gfs(v, u, x.second, x.second, 0, (a[u] + a[v]) % mod, 1);
    }
    for (pair<int, int> &x : e[u]) {
      int v = x.first;
      if (vis[v]) continue;
      gfs(v, u, x.second, x.second, 0, (a[u] + a[v]) % mod, -1);
    }
    for (pair<int, int> &x : e[u]) {
      int v = x.first;
      if (vis[v]) continue;
      rt = 0;
      sum = si[v];
      getrt(v, 0);
      sol(rt);
    }
  }
  int main() {
    scanf("%d%d", &n, &k);
    for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
    for (int i = 1; i < n; i++) {
      int u, v, w;
      scanf("%d%d%d", &u, &v, &w);
      e[u].push_back({v, w});
      e[v].push_back({u, w});
    }
    f[0] = 1e9;
    sum = n;
    getrt(1, 0);
    sol(rt);
    printf("%lld\n", ans);
  }
  