#include <bits/stdc++.h>
using namespace std;
const int MODU = 1000000007;
vector<int> adj[2001];
bool check[2001];
int a[2001];
int n, k;
long dfs(int u, int p, int root) {
  if ((a[u] == a[root]) && (check[u])) return 0;
  if ((a[u] < a[root]) || (a[u] > a[root] + k)) return 0;
  long long res = 1;
  for (int i = 0; i < adj[u].size(); i++) {
    int v = adj[u][i];
    if (v == p) continue;
    long long tmp = dfs(v, u, root) + 1;
    res = (res * tmp) % MODU;
  }
  return res;
}
int main() {
  cin >> k >> n;
  long long res = 0;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }
  for (int i = 1; i <= n; i++) {
    res = (res + dfs(i, -1, i)) % MODU;
    check[i] = true;
  }
  cout << res << endl;
}
