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>
  template <class t>
  inline void read(t &s) {
    s = 0;
    signed f = 1;
    char c = getchar();
    while (!isdigit(c)) {
      if (c == '-') f = -1;
      c = getchar();
    }
    while (isdigit(c)) s = (s << 3) + (s << 1) + (c ^ 48), c = getchar();
    ;
    s *= f;
  }
  template <class t, class... A>
  inline void read(t &x, A &...a) {
    read(x);
    read(a...);
  }
  template <class t>
  inline void write(t x) {
    if (x < 0) putchar('-'), x = -x;
    static int buf[50], top = 0;
    while (x) buf[++top] = x % 10, x /= 10;
    if (!top) buf[++top] = 0;
    while (top) putchar(buf[top--] ^ '0');
  }
  inline void setIn(std::string s) {
    freopen(s.c_str(), "r", stdin);
    return;
  }
  inline void setOut(std::string s) {
    freopen(s.c_str(), "w", stdout);
    return;
  }
  inline void setIO(std::string s = "") {
    setIn(s + ".in");
    setOut(s + ".out");
    return;
  }
  template <class t>
  inline bool ckmin(t &x, t y) {
    if (x > y) {
      x = y;
      return 1;
    }
    return 0;
  }
  template <class t>
  inline bool ckmax(t &x, t y) {
    if (x < y) {
      x = y;
      return 1;
    }
    return 0;
  }
  inline int lowbit(int x) { return x & (-x); }
  const int MaxN = 2e5 + 50;
  const int p = 1e9 + 7;
  int a[MaxN], n, K;
  inline int plus(const int &x, const int &y) {
    int s = x + y - p;
    return s + (s >> 31 & p);
  }
  inline void add(int &x, const int &y) { (x += y) >= p && (x -= p); }
  struct BIT {
    int tr[MaxN];
    inline void add(int x, int k) {
      ++x;
      while (x <= n) ::add(tr[x], k), x += lowbit(x);
    }
    inline int ask(int x) {
      int ret = 0;
      ++x;
      while (x) ::add(ret, tr[x]), x ^= lowbit(x);
      return ret;
    }
    inline void del(int x) {
      ++x;
      while (x <= n) tr[x] = 0, x += lowbit(x);
    }
  } T[2], C[2];
  std::vector<std::pair<int, int> > E[MaxN];
  int siz[MaxN], vis[MaxN], root, mnsiz, allsiz, ans;
  inline void getroot(int u, int fa) {
    siz[u] = 1;
    int mx = 0;
    for (auto v : E[u])
      if (v.first != fa && !vis[v.first])
        getroot(v.first, u), ckmax(mx, siz[v.first]), siz[u] += siz[v.first];
    ckmax(mx, allsiz - siz[u]);
    if (ckmin(mnsiz, mx)) root = u;
  }
  std::vector<std::pair<int, int> > dr1[2], dr2[2];
  inline void go(int u, int fa, int s, int cnt, int col, int st) {
    if (cnt > K) return;
    dr1[st].push_back(std::make_pair(cnt, s));
    for (auto v : E[u])
      if (v.first != fa && !vis[v.first])
        go(v.first, u, plus(s, a[v.first]), cnt + (v.second != col), v.second,
           st);
  }
  inline void solve(int u) {
    for (auto v : E[u])
      if (!vis[v.first]) {
        go(v.first, u, a[v.first], 0, v.second, v.second);
        for (int o = (0); o <= (1); ++o)
          for (auto i : dr1[o])
            for (int r = (0); r <= (1); ++r)
              if (K - i.first - (o ^ r) >= 0)
                ans = (ans + (T[r].ask(K - i.first - (o ^ r)) +
                              1LL * C[r].ask(K - i.first - (o ^ r)) * i.second)) %
                      p;
        for (int o = (0); o <= (1); ++o)
          for (auto i : dr1[o])
            add(ans, plus(i.second, a[u])), dr2[o].push_back(i),
                T[o].add(i.first, plus(i.second, a[u])), C[o].add(i.first, +1);
        dr1[0].clear(), dr1[1].clear();
      }
    add(ans, a[u]);
    for (int o = (0); o <= (1); ++o)
      for (auto i : dr2[o]) T[o].del(i.first), C[o].del(i.first);
    dr2[0].clear(), dr2[1].clear();
  }
  inline void divide(int u) {
    vis[u] = 1, solve(u);
    for (auto v : E[u])
      if (!vis[v.first])
        allsiz = siz[v.first], mnsiz = 1e9, getroot(v.first, u), getroot(root, 0),
        divide(root);
  }
  signed main(void) {
    read(n, K);
    for (int i = (1); i <= (n); ++i) read(a[i]);
    int u, v, w;
    for (int i = (1); i <= (n - 1); ++i)
      read(u, v, w), E[u].push_back(std::make_pair(v, w)),
          E[v].push_back(std::make_pair(u, w));
    allsiz = n, mnsiz = 1e9, getroot(1, 0), getroot(root, 0), divide(root);
    write(ans), putchar('\n');
    return 0;
  }
  