#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 10;
const int M = 1000000007;
void add(int &x, int y) {
  x += y;
  if (x >= M) x -= M;
}
void sub(int &x, int y) {
  x -= y;
  if (x < 0) x += M;
}
bool ban[maxn];
int n, k, a[maxn], sz[maxn], ans, cnt[maxn], way[maxn], mx, tc[maxn], tw[maxn];
int scnt[2][maxn], sway[2][maxn];
vector<pair<int, int> > G[maxn];
void dfs(int u, int fa) {
  sz[u] = 1;
  for (auto e : G[u]) {
    int v = e.first;
    if (ban[v]) continue;
    if (v == fa) continue;
    dfs(v, u);
    sz[u] += sz[v];
  }
}
void dfs2(int u, int fa, int o, int sum, int dep) {
  if (dep > k) return;
  mx = max(mx, dep);
  add(way[dep], 1);
  add(cnt[dep], sum);
  for (auto e : G[u]) {
    int v = e.first;
    if (ban[v] || v == fa) continue;
    dfs2(v, u, e.second, (sum + a[v]) % M, dep + (e.second ^ o));
  }
}
int work(int *cnt, int *way, int *cnt2, int *way2, int val, int mx, int k) {
  if (k < 0) return 0;
  mx = min(mx, k);
  for (int i = 0; i <= mx; i++) {
    tc[i] = cnt[i];
    tw[i] = way[i];
    if (i) {
      add(tc[i], tc[i - 1]);
      add(tw[i], tw[i - 1]);
    }
  }
  int ret = 0;
  for (int i = 0; i <= mx; i++) {
    int j = min(mx, k - i);
    add(ret, 1ll * val * way2[i] % M * tw[j] % M);
    add(ret, 1ll * cnt2[i] * tw[j] % M);
    add(ret, 1ll * way2[i] * tc[j] % M);
  }
  return ret;
}
void solve(int u) {
  dfs(u, 0);
  int r = u;
  while (1) {
    int tmp = 0;
    for (auto e : G[u]) {
      int v = e.first;
      if (ban[v] || sz[v] > sz[u]) continue;
      if (sz[v] * 2 > sz[r]) tmp = v;
    }
    if (!tmp) break;
    u = tmp;
  }
  int SZ = min(sz[r], k);
  for (int i = 0; i <= SZ; i++) {
    scnt[0][i] = scnt[1][i] = 0;
    sway[0][i] = sway[1][i] = 0;
  }
  for (auto e : G[u]) {
    int v = e.first;
    if (ban[v]) continue;
    int o = e.second;
    mx = 0;
    dfs2(v, u, o, a[v], 0);
    for (int i = 0; i <= mx; i++) {
      add(ans, 2ll * way[i] * a[u] % M);
      add(ans, 2 * cnt[i] % M);
    }
    sub(ans, work(cnt, way, cnt, way, a[u], mx, k));
    for (int i = 0; i <= mx; i++) {
      add(scnt[o][i], cnt[i]);
      add(sway[o][i], way[i]);
      cnt[i] = 0;
      way[i] = 0;
    }
  }
  add(ans, work(scnt[0], sway[0], scnt[0], sway[0], a[u], SZ, k));
  add(ans, work(scnt[1], sway[1], scnt[1], sway[1], a[u], SZ, k));
  add(ans, work(scnt[0], sway[0], scnt[1], sway[1], a[u], SZ, k - 1) * 2 % M);
  ban[u] = 1;
  for (auto e : G[u]) {
    int v = e.first;
    if (ban[v]) continue;
    solve(v);
  }
}
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i < n; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    G[u].push_back((pair<int, int>){v, w});
    G[v].push_back((pair<int, int>){u, w});
  }
  solve(1);
  ans = 1ll * ans * (M + 1) / 2 % M;
  for (int i = 1; i <= n; i++) add(ans, a[i]);
  cout << ans << endl;
}

### Prompt

Your task is to create a Cpp solution to the following problem:

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 maxn = 2e5 + 10;
const int M = 1000000007;
void add(int &x, int y) {
  x += y;
  if (x >= M) x -= M;
}
void sub(int &x, int y) {
  x -= y;
  if (x < 0) x += M;
}
bool ban[maxn];
int n, k, a[maxn], sz[maxn], ans, cnt[maxn], way[maxn], mx, tc[maxn], tw[maxn];
int scnt[2][maxn], sway[2][maxn];
vector<pair<int, int> > G[maxn];
void dfs(int u, int fa) {
  sz[u] = 1;
  for (auto e : G[u]) {
    int v = e.first;
    if (ban[v]) continue;
    if (v == fa) continue;
    dfs(v, u);
    sz[u] += sz[v];
  }
}
void dfs2(int u, int fa, int o, int sum, int dep) {
  if (dep > k) return;
  mx = max(mx, dep);
  add(way[dep], 1);
  add(cnt[dep], sum);
  for (auto e : G[u]) {
    int v = e.first;
    if (ban[v] || v == fa) continue;
    dfs2(v, u, e.second, (sum + a[v]) % M, dep + (e.second ^ o));
  }
}
int work(int *cnt, int *way, int *cnt2, int *way2, int val, int mx, int k) {
  if (k < 0) return 0;
  mx = min(mx, k);
  for (int i = 0; i <= mx; i++) {
    tc[i] = cnt[i];
    tw[i] = way[i];
    if (i) {
      add(tc[i], tc[i - 1]);
      add(tw[i], tw[i - 1]);
    }
  }
  int ret = 0;
  for (int i = 0; i <= mx; i++) {
    int j = min(mx, k - i);
    add(ret, 1ll * val * way2[i] % M * tw[j] % M);
    add(ret, 1ll * cnt2[i] * tw[j] % M);
    add(ret, 1ll * way2[i] * tc[j] % M);
  }
  return ret;
}
void solve(int u) {
  dfs(u, 0);
  int r = u;
  while (1) {
    int tmp = 0;
    for (auto e : G[u]) {
      int v = e.first;
      if (ban[v] || sz[v] > sz[u]) continue;
      if (sz[v] * 2 > sz[r]) tmp = v;
    }
    if (!tmp) break;
    u = tmp;
  }
  int SZ = min(sz[r], k);
  for (int i = 0; i <= SZ; i++) {
    scnt[0][i] = scnt[1][i] = 0;
    sway[0][i] = sway[1][i] = 0;
  }
  for (auto e : G[u]) {
    int v = e.first;
    if (ban[v]) continue;
    int o = e.second;
    mx = 0;
    dfs2(v, u, o, a[v], 0);
    for (int i = 0; i <= mx; i++) {
      add(ans, 2ll * way[i] * a[u] % M);
      add(ans, 2 * cnt[i] % M);
    }
    sub(ans, work(cnt, way, cnt, way, a[u], mx, k));
    for (int i = 0; i <= mx; i++) {
      add(scnt[o][i], cnt[i]);
      add(sway[o][i], way[i]);
      cnt[i] = 0;
      way[i] = 0;
    }
  }
  add(ans, work(scnt[0], sway[0], scnt[0], sway[0], a[u], SZ, k));
  add(ans, work(scnt[1], sway[1], scnt[1], sway[1], a[u], SZ, k));
  add(ans, work(scnt[0], sway[0], scnt[1], sway[1], a[u], SZ, k - 1) * 2 % M);
  ban[u] = 1;
  for (auto e : G[u]) {
    int v = e.first;
    if (ban[v]) continue;
    solve(v);
  }
}
int main() {
  cin >> n >> k;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i < n; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    G[u].push_back((pair<int, int>){v, w});
    G[v].push_back((pair<int, int>){u, w});
  }
  solve(1);
  ans = 1ll * ans * (M + 1) / 2 % M;
  for (int i = 1; i <= n; i++) add(ans, a[i]);
  cout << ans << endl;
}

```