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;
  using ll = long long;
  const ll mod = 1e9 + 7;
  const int maxn = 2e5 + 50;
  int n, k;
  ll a[maxn];
  struct edge_node {
    int to, nxt;
    int w;
  } edge[maxn << 2];
  int head[maxn], id = 2;
  inline void add_edge(int u, int v, int w) {
    edge[id] = edge_node{v, head[u], w};
    head[u] = id++;
    edge[id] = edge_node{u, head[v], w};
    head[v] = id++;
  }
  int wc;
  bool st[maxn];
  int get_sz(int u, int pre) {
    if (st[u]) {
      return 0;
    }
    int res = 1;
    for (int i = head[u]; i; i = edge[i].nxt) {
      int v = edge[i].to;
      if (v != pre) {
        res += get_sz(v, u);
      }
    }
    return res;
  }
  int get_wc(int u, int pre, int tot) {
    if (st[u]) {
      return 0;
    }
    int sz = 1, mx = 0;
    for (int i = head[u]; i; i = edge[i].nxt) {
      int v = edge[i].to;
      if (v != pre) {
        int szv = get_wc(v, u, tot);
        mx = max(mx, szv);
        sz += szv;
      }
    }
    mx = max(mx, tot - sz);
    if (mx <= tot / 2) {
      wc = u;
    }
    return sz;
  }
  struct BIT {
    ll c[maxn];
    inline static int lowbit(int x) { return x & (-x); }
    void modify(int x, ll val) {
      while (x < maxn) {
        c[x] = (c[x] + val) % mod;
        x += lowbit(x);
      }
    }
    ll get_sum(int x) {
      ll res = 0;
      while (x) {
        res = (res + c[x]) % mod;
        x -= lowbit(x);
      }
      return res;
    }
    BIT() { memset(c, 0, sizeof(c)); }
  } bit[2];
  struct BIT2 {
    ll c[maxn];
    inline static int lowbit(int x) { return x & (-x); }
    void modify(int x, ll val) {
      while (x < maxn) {
        c[x] += val;
        x += lowbit(x);
      }
    }
    ll get_sum(int x) {
      ll res = 0;
      while (x) {
        res += c[x];
        x -= lowbit(x);
      }
      return res;
    }
    BIT2() { memset(c, 0, sizeof(c)); }
  } bit2[2];
  ll calc2(int u, int pre, int pre_clr, int clr_num, ll val) {
    if (st[u]) {
      return 0;
    }
    if (clr_num > k) {
      return 0;
    }
    val = (val + a[u]) % mod;
    ll res = val;
    for (int i = head[u]; i; i = edge[i].nxt) {
      int v = edge[i].to;
      int w = edge[i].w;
      if (v != pre) {
        ll tmp = calc2(v, u, w, clr_num + (pre_clr != w), val);
        res = (res + tmp) % mod;
      }
    }
    return res;
  }
  void count(int u, int pre, int pre_clr, int clr_num, ll val, int st_clr) {
    if (st[u]) {
      return;
    }
    if (clr_num > k) {
      return;
    }
    val = (val + a[u]) % mod;
    bit[st_clr].modify(clr_num + 1, val);
    bit2[st_clr].modify(clr_num + 1, 1);
    for (int i = head[u]; i; i = edge[i].nxt) {
      int v = edge[i].to;
      int w = edge[i].w;
      if (v != pre) {
        count(v, u, w, clr_num + (pre_clr != w), val, st_clr);
      }
    }
  }
  void del(int u, int pre, int pre_clr, int clr_num, ll val, int st_clr) {
    if (st[u]) {
      return;
    }
    if (clr_num > k) {
      return;
    }
    val = (val + a[u]) % mod;
    bit[st_clr].modify(clr_num + 1, (-val % mod + mod) % mod);
    bit2[st_clr].modify(clr_num + 1, -1);
    for (int i = head[u]; i; i = edge[i].nxt) {
      int v = edge[i].to;
      int w = edge[i].w;
      if (v != pre) {
        del(v, u, w, clr_num + (pre_clr != w), val, st_clr);
      }
    }
  }
  ll calc3(int u, int pre, int pre_clr, int clr_num, ll val, int st_clr) {
    if (st[u]) {
      return 0;
    }
    if (clr_num > k) {
      return 0;
    }
    val = (val + a[u]) % mod;
    ll res = 0;
    ll tmp = bit[st_clr].get_sum(k - clr_num + 1);
    ll cnt = bit2[st_clr].get_sum(k - clr_num + 1);
    res = (res + tmp) % mod;
    res = (res + val * cnt % mod) % mod;
    if (clr_num <= k - 1) {
      ll tmp = bit[!st_clr].get_sum(k - clr_num - 1 + 1);
      ll cnt = bit2[!st_clr].get_sum(k - clr_num - 1 + 1);
      res = (res + tmp) % mod;
      res = (res + val * cnt % mod) % mod;
    }
    for (int i = head[u]; i; i = edge[i].nxt) {
      int v = edge[i].to;
      int w = edge[i].w;
      if (v != pre) {
        ll tmp = calc3(v, u, w, clr_num + (pre_clr != w), val, st_clr);
        res = (res + tmp) % mod;
      }
    }
    return res;
  }
  inline ll tydq(int u) {
    if (st[u]) {
      return 0;
    }
    ll res = 0;
    wc = u;
    get_wc(u, 0, get_sz(u, 0));
    u = wc;
    st[u] = true;
    for (int i = head[u]; i; i = edge[i].nxt) {
      int v = edge[i].to;
      int w = edge[i].w;
      res = (res + calc3(v, u, w, 0, a[u], w)) % mod;
      res = (res + calc2(v, u, w, 0, a[u])) % mod;
      count(v, u, w, 0, 0, w);
    }
    for (int i = head[u]; i; i = edge[i].nxt) {
      int v = edge[i].to;
      int w = edge[i].w;
      del(v, u, w, 0, 0, w);
    }
    for (int i = head[u]; i; i = edge[i].nxt) {
      int v = edge[i].to;
      res = (res + tydq(v)) % mod;
    }
    return res;
  }
  signed main() {
    std::cin.tie(nullptr)->sync_with_stdio(false);
    ll ans = 0;
    cin >> n >> k;
    for (int i = 1; i <= n; ++i) {
      cin >> a[i];
      ans = (ans + a[i]) % mod;
    }
    for (int i = 1; i <= n - 1; ++i) {
      int u, v, w;
      cin >> u >> v >> w;
      add_edge(u, v, w);
    }
    ll res = tydq(1);
    ans = (ans + res) % mod;
    cout << ans << '\n';
    return 0;
  }
  