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 maxn = 410000;
  const int mod = 1e9 + 7;
  struct Tree {
    long long C[maxn];
    int n;
    void init(int N) {
      this->n = N + 4;
      memset(C, 0, sizeof(C[0]) * (n + 4));
    }
    void add(int x, long long d) {
      x += 1;
      for (int i = x; i <= n; i += ((i) & (-i))) C[i] = (C[i] + d) % mod;
    }
    long long sum(int x) {
      if (x < 0) return 0;
      x += 1;
      x = min(x, n);
      long long ret = 0;
      for (int i = x; i > 0; i -= ((i) & (-i))) ret += C[i];
      return ret % mod;
    }
  } value[2], times[2];
  vector<pair<int, int>> G[maxn];
  int vis[maxn], siz[maxn], f[maxn], pos, sum;
  int n, k, a[maxn];
  long long answer = 0;
  void getroot(int x, int fa) {
    f[x] = 0;
    siz[x] = 1;
    for (auto [y, w] : G[x])
      if (y != fa && !vis[y]) {
        getroot(y, x);
        f[x] = max(f[x], siz[y]);
        siz[x] += siz[y];
      }
    f[x] = max(f[x], sum - siz[x]);
    if (f[x] < f[pos]) pos = x;
  }
  void dfs1(int x, int fa, long long w, int color, int cnt, int start) {
    w = (w + a[x]) % mod;
    answer += value[start].sum(k - cnt) % mod;
    answer += times[start].sum(k - cnt) * w % mod;
    answer += value[start ^ 1].sum(k - cnt - 1) % mod;
    answer += times[start ^ 1].sum(k - cnt - 1) * w % mod;
    for (auto [y, t] : G[x])
      if (y != fa && !vis[y]) {
        dfs1(y, x, w, t, cnt + (t != color), start);
      }
  }
  void dfs2(int x, int fa, long long w, int color, int cnt, int start) {
    w = (w + a[x]) % mod;
    if (cnt <= k) {
      answer += w;
      value[start].add(cnt, w);
      times[start].add(cnt, 1);
    }
    for (auto [y, t] : G[x])
      if (y != fa && !vis[y]) {
        dfs2(y, x, w, t, cnt + (t != color), start);
      }
  }
  int travel(int x, int fa) {
    int ret = 1;
    for (auto [y, t] : G[x])
      if (y != fa && !vis[y]) {
        ret += travel(y, x);
      }
    return ret;
  }
  void calc(int x) {
    answer += a[x];
    const int size = travel(x, -1) + 1;
    for (int i = 0; i < 2; ++i) {
      value[i].init(size);
      times[i].init(size);
    }
    for (auto [y, t] : G[x])
      if (!vis[y]) {
        dfs1(y, x, 0, t, 0, t);
        dfs2(y, x, a[x], t, 0, t);
      }
    answer %= mod;
  }
  void solve(int x, int cnt) {
    pos = maxn - 1;
    f[pos] = sum = cnt;
    getroot(x, -1);
    int root = pos;
    vis[root] = 1;
    calc(root);
    for (auto [y, w] : G[root])
      if (!vis[y]) solve(y, siz[y] < siz[root] ? siz[y] : cnt - siz[root]);
  }
  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 x, y, w;
      scanf("%d %d %d", &x, &y, &w);
      G[x].emplace_back(y, w);
      G[y].emplace_back(x, w);
    }
    solve(1, n);
    answer %= mod;
    printf("%lld\n", answer);
    return 0;
  }
  