structEdge { int to, next; } e[maxn * 2]; int c1, head[maxn]; inlinevoidadd_edge(int u, int v){ e[c1].to = v; e[c1].next = head[u]; head[u] = c1++; }
intdfs(int u, int fa, int o){ if (a[u] < a[o] - d || a[u] > a[o] || a[u] == a[o] && u > o) return0; int ans = 1; for (int i = head[u]; ~i; i = e[i].next) { int v = e[i].to; if (v == fa) continue; ans = 1ll * ans * (dfs(v, u, o) + 1) % p; } return ans; }
intmain(){ fill(head, head + maxn, -1); ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
cin >> d >> n; int ans = 0; for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = 1; i < n; ++i) { int x, y; cin >> x >> y; add_edge(x, y); add_edge(y, x); } for (int i = 1; i <= n; ++i) ans = (ans + dfs(i, 0, i)) % p; cout << ans << "\n"; return0; }