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 + 5, mod = 1e9 + 7;
  vector<pair<int, int>> g[N];
  int a[N], sz[N], big[N], die[N];
  void get_sz(int v, int par = -1) {
    sz[v] = 1;
    big[v] = -1;
    for (auto& e : g[v]) {
      int u = e.first;
      if (u == par || die[u]) continue;
      get_sz(u, v);
      if (big[v] == -1 || sz[u] > sz[big[v]]) big[v] = u;
      sz[v] += sz[u];
    }
  }
  void add(long long& var, long long val) {
    var += val;
    if (var >= mod)
      var -= mod;
    else if (var < 0)
      var += mod;
  }
  long long ans = 0;
  struct Fenwick {
    long long sum[N];
    Fenwick() { memset(sum, 0, sizeof sum); }
    long long get(int x) {
      long long ret = 0;
      for (++x; x; x -= x & -x) add(ret, sum[x]);
      return ret;
    }
    void update(int x, int v) {
      for (++x; x < N; x += x & -x) add(sum[x], v);
    }
  };
  Fenwick sum[2];
  Fenwick cnt[2];
  int mul = 1;
  void dfs(int v, int par, long long cost, int change, int col) {
    int last = col ^ (change & 1);
    add(cost, a[v]);
    sum[col].update(change, mul * cost);
    cnt[col].update(change, mul);
    for (auto& e : g[v]) {
      int u = e.first;
      if (u == par || die[u]) continue;
      dfs(u, v, cost, change + (last != e.second), col);
    }
  }
  int n, k;
  void dfs2(int v, int par, long long cost, int change, int col) {
    if (change > k) return;
    int last = col ^ (change & 1);
    add(cost, a[v]);
    ans = (ans + 1LL * cnt[col].get(k - change) * cost +
           1LL * sum[col].get(k - change));
    if (change + 1 <= k)
      ans = (ans + 1LL * cnt[col ^ 1].get(k - change - 1) * cost +
             1LL * sum[col ^ 1].get(k - change - 1));
    for (auto& e : g[v]) {
      int u = e.first;
      if (u == par || die[u]) continue;
      dfs2(u, v, cost, change + (last != e.second), col);
    }
  }
  void centroid(int v) {
    get_sz(v);
    int tot = sz[v];
    while (big[v] != -1 && sz[big[v]] * 2 >= tot) v = big[v];
    for (auto& e : g[v]) {
      int u = e.first;
      if (die[u]) continue;
      dfs2(u, v, 0, 0, e.second);
      mul = 1;
      dfs(u, v, a[v], 0, e.second);
    }
    ans = (ans + a[v]) % mod;
    for (int i = 0; i < 2; ++i) {
      ans = (ans + 1LL * sum[i].get(k)) % mod;
    }
    for (auto& e : g[v]) {
      int u = e.first;
      if (die[u]) continue;
      mul = -1;
      dfs(u, v, a[v], 0, e.second);
    }
    die[v] = 1;
    for (auto& e : g[v]) {
      int u = e.first;
      if (die[u]) continue;
      centroid(u);
    }
  }
  int main() {
    scanf("%d %d", &n, &k);
    for (int i = 0; i < n; ++i) {
      scanf("%d", a + i);
    }
    for (int i = 0; i < n - 1; ++i) {
      int u, v, w;
      scanf("%d %d %d", &u, &v, &w);
      --u, --v;
      g[u].emplace_back(v, w);
      g[v].emplace_back(u, w);
    }
    centroid(0);
    ans %= mod;
    if (ans < 0) ans += mod;
    printf("%lld\n", ans);
    return 0;
  }
  