#include <bits/stdc++.h>
using namespace std;
const int p = 1e9 + 7;
const int N = 2e5 + 10;
int n, k, a[N];
vector<pair<int, int> > g[N];
inline void A(int &x, int y) { x = (x + y >= p ? x + y - p : x + y); }
inline void U(int &x, int y) { x = (x > y ? x : y); }
int del[N], si[N];
int mnsi, mnid;
inline void GetSi(int x, int fa) {
  si[x] = 1;
  for (auto e : g[x])
    if (e.second != fa && !del[e.second]) {
      GetSi(e.second, x);
      si[x] += si[e.second];
    }
}
inline void GetMx(int x, int fa, int al) {
  int mx = al - si[x];
  for (auto e : g[x])
    if (e.second != fa && !del[e.second]) {
      mx = max(mx, si[e.second]);
      GetMx(e.second, x, al);
    }
  if (mx < mnsi) mnsi = mx, mnid = x;
}
struct DS {
  int f[N];
  inline void Add(int x, int y) {
    for (int i = x; i <= n; i += (i & -i)) A(f[i], y);
  }
  inline void Clr(int x) {
    for (int i = x; i <= n; i += (i & -i)) f[i] = 0;
  }
  inline int Ask(int x) {
    int ans = 0;
    for (int i = x; i; i -= (i & -i)) A(ans, f[i]);
    return ans;
  }
} f1, g1, f2, g2;
int sw1, sw2;
int ans;
void PushIn(int x, int fa, int fe, int sm, int sw, DS &f, DS &G, int &mxsw) {
  if (sw > k) return;
  mxsw = max(mxsw, sw);
  A(sm, a[x]);
  f.Add(sw + 1, sm);
  G.Add(sw + 1, 1);
  for (auto e : g[x]) {
    int y = e.second, z = e.first;
    if (del[y] || y == fa) continue;
    PushIn(y, x, z, sm, sw + (fe != z), f, G, mxsw);
  }
}
void Calc(int x, int fa, int fe, int sm, int sw) {
  if (sw > k) return;
  A(sm, a[x]);
  if (sw < k)
    ans = (ans + 1ll * sm * (g1.Ask(k - sw) - g2.Ask(k - sw)) +
           (f1.Ask(k - sw) - f2.Ask(k - sw))) %
          p;
  ans = (ans + 1ll * sm * g2.Ask(k - sw + 1) + f2.Ask(k - sw + 1)) % p;
  for (auto e : g[x]) {
    int y = e.second, z = e.first;
    if (del[y] || y == fa) continue;
    Calc(y, x, z, sm, sw + (fe != z));
  }
}
inline void Solve(int x) {
  GetSi(x, 0);
  mnsi = 1e9;
  GetMx(x, 0, si[x]);
  int u = mnid;
  sw1 = 0, sw2 = 0;
  f1.Add(1, a[u]);
  f2.Add(1, a[u]);
  g1.Add(1, 1);
  g2.Add(1, 1);
  A(ans, a[u]);
  int la = -1;
  for (auto e : g[u]) {
    int y = e.second, z = e.first;
    if (z != la) {
      for (int i = 1; i <= sw2 + 1; ++i) f2.Clr(i), g2.Clr(i);
      sw2 = 0;
      la = z;
      f2.Add(1, a[u]);
      g2.Add(1, 1);
    }
    if (del[y]) continue;
    Calc(y, u, z, 0, 0);
    PushIn(y, u, z, a[u], 0, f1, g1, sw1);
    PushIn(y, u, z, a[u], 0, f2, g2, sw2);
  }
  for (int i = 1; i <= sw1 + 1; ++i) f1.Clr(i), g1.Clr(i);
  for (int i = 1; i <= sw2 + 1; ++i) f2.Clr(i), g2.Clr(i);
  del[u] = 1;
  for (auto e : g[u])
    if (!del[e.second]) Solve(e.second);
}
inline void Dfs(int x, int fa, int fe, int sw, int sm, int rt) {
  if (sw > k) return;
  A(sm, a[x]);
  if (x <= rt) A(ans, p - sm);
  for (auto e : g[x]) {
    int y = e.second, z = e.first;
    if (y == fa) continue;
    Dfs(y, x, z, sw + (fe != z), sm, rt);
  }
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; ++i) scanf("%d", a + i);
  for (int i = 1, x, y, z; i < n; ++i) {
    scanf("%d%d%d", &x, &y, &z);
    g[x].push_back(pair<int, int>(z, y));
    g[y].push_back(pair<int, int>(z, x));
  }
  for (int i = 1; i <= n; ++i) sort(g[i].begin(), g[i].end());
  Solve(1);
  if (ans < 0) ans += p;
  printf("%d\n", ans);
  return 0;
}

### Prompt

Please provide a Cpp coded solution to the problem described below:

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

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
const int p = 1e9 + 7;
const int N = 2e5 + 10;
int n, k, a[N];
vector<pair<int, int> > g[N];
inline void A(int &x, int y) { x = (x + y >= p ? x + y - p : x + y); }
inline void U(int &x, int y) { x = (x > y ? x : y); }
int del[N], si[N];
int mnsi, mnid;
inline void GetSi(int x, int fa) {
  si[x] = 1;
  for (auto e : g[x])
    if (e.second != fa && !del[e.second]) {
      GetSi(e.second, x);
      si[x] += si[e.second];
    }
}
inline void GetMx(int x, int fa, int al) {
  int mx = al - si[x];
  for (auto e : g[x])
    if (e.second != fa && !del[e.second]) {
      mx = max(mx, si[e.second]);
      GetMx(e.second, x, al);
    }
  if (mx < mnsi) mnsi = mx, mnid = x;
}
struct DS {
  int f[N];
  inline void Add(int x, int y) {
    for (int i = x; i <= n; i += (i & -i)) A(f[i], y);
  }
  inline void Clr(int x) {
    for (int i = x; i <= n; i += (i & -i)) f[i] = 0;
  }
  inline int Ask(int x) {
    int ans = 0;
    for (int i = x; i; i -= (i & -i)) A(ans, f[i]);
    return ans;
  }
} f1, g1, f2, g2;
int sw1, sw2;
int ans;
void PushIn(int x, int fa, int fe, int sm, int sw, DS &f, DS &G, int &mxsw) {
  if (sw > k) return;
  mxsw = max(mxsw, sw);
  A(sm, a[x]);
  f.Add(sw + 1, sm);
  G.Add(sw + 1, 1);
  for (auto e : g[x]) {
    int y = e.second, z = e.first;
    if (del[y] || y == fa) continue;
    PushIn(y, x, z, sm, sw + (fe != z), f, G, mxsw);
  }
}
void Calc(int x, int fa, int fe, int sm, int sw) {
  if (sw > k) return;
  A(sm, a[x]);
  if (sw < k)
    ans = (ans + 1ll * sm * (g1.Ask(k - sw) - g2.Ask(k - sw)) +
           (f1.Ask(k - sw) - f2.Ask(k - sw))) %
          p;
  ans = (ans + 1ll * sm * g2.Ask(k - sw + 1) + f2.Ask(k - sw + 1)) % p;
  for (auto e : g[x]) {
    int y = e.second, z = e.first;
    if (del[y] || y == fa) continue;
    Calc(y, x, z, sm, sw + (fe != z));
  }
}
inline void Solve(int x) {
  GetSi(x, 0);
  mnsi = 1e9;
  GetMx(x, 0, si[x]);
  int u = mnid;
  sw1 = 0, sw2 = 0;
  f1.Add(1, a[u]);
  f2.Add(1, a[u]);
  g1.Add(1, 1);
  g2.Add(1, 1);
  A(ans, a[u]);
  int la = -1;
  for (auto e : g[u]) {
    int y = e.second, z = e.first;
    if (z != la) {
      for (int i = 1; i <= sw2 + 1; ++i) f2.Clr(i), g2.Clr(i);
      sw2 = 0;
      la = z;
      f2.Add(1, a[u]);
      g2.Add(1, 1);
    }
    if (del[y]) continue;
    Calc(y, u, z, 0, 0);
    PushIn(y, u, z, a[u], 0, f1, g1, sw1);
    PushIn(y, u, z, a[u], 0, f2, g2, sw2);
  }
  for (int i = 1; i <= sw1 + 1; ++i) f1.Clr(i), g1.Clr(i);
  for (int i = 1; i <= sw2 + 1; ++i) f2.Clr(i), g2.Clr(i);
  del[u] = 1;
  for (auto e : g[u])
    if (!del[e.second]) Solve(e.second);
}
inline void Dfs(int x, int fa, int fe, int sw, int sm, int rt) {
  if (sw > k) return;
  A(sm, a[x]);
  if (x <= rt) A(ans, p - sm);
  for (auto e : g[x]) {
    int y = e.second, z = e.first;
    if (y == fa) continue;
    Dfs(y, x, z, sw + (fe != z), sm, rt);
  }
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; ++i) scanf("%d", a + i);
  for (int i = 1, x, y, z; i < n; ++i) {
    scanf("%d%d%d", &x, &y, &z);
    g[x].push_back(pair<int, int>(z, y));
    g[y].push_back(pair<int, int>(z, x));
  }
  for (int i = 1; i <= n; ++i) sort(g[i].begin(), g[i].end());
  Solve(1);
  if (ans < 0) ans += p;
  printf("%d\n", ans);
  return 0;
}

```