#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O2,no-stack-protector,unroll-loops,fast-math")
const long long maxn = 1e5 + 10, maxm = 5e2 + 10, lg = 18, mod = 1e9 + 7,
                inf = 1e18;
long long n, MOD, k, x, w[maxn], ans, in[maxn], out[maxn], rt, siz[maxn],
    msiz[maxn], powk[maxn], invk[maxn];
vector<long long> g[maxn];
vector<pair<long long, long long> > I, O, II, OO;
bool mk[maxn];
long long pwm(long long a, long long b) {
  long long ret = 1;
  for (; b; a = a * a % MOD, b >>= 1)
    if (b & 1) ret *= a, ret %= MOD;
  return ret;
}
void rfs(long long v, long long p, long long kol) {
  siz[v] = 1;
  for (auto u : g[v])
    if (u != p && !mk[u]) rfs(u, v, kol), siz[v] += siz[u];
  msiz[v] = max(siz[v], kol - siz[v]);
  if (msiz[v] > msiz[rt]) rt = v;
}
void dfs(long long v, long long p, long long dep, long long w1, long long w2) {
  w1 = (w1 * k + w[v]) % MOD, w2 = (w2 + w[v] * powk[dep]) % MOD;
  long long _w1 = (x - w1 + MOD) * invk[dep] % MOD;
  II.push_back(make_pair(_w1, v)), OO.push_back(make_pair(w2, v));
  for (auto u : g[v])
    if (u != p && !mk[u]) dfs(u, v, dep + 1, w1, w2);
}
void modif(vector<pair<long long, long long> > &a,
           vector<pair<long long, long long> > &b, long long o) {
  sort(a.begin(), a.end()), sort(b.begin(), b.end());
  for (auto p : a) {
    long long w = p.first, id = p.second;
    out[id] += o * (lower_bound(b.begin(), b.end(), make_pair(w + 1, 0ll)) -
                    b.begin());
    out[id] -=
        o * (lower_bound(b.begin(), b.end(), make_pair(w, 0ll)) - b.begin());
  }
  for (auto p : b) {
    long long w = p.first, id = p.second;
    in[id] += o * (lower_bound(a.begin(), a.end(), make_pair(w + 1, 0ll)) -
                   a.begin());
    in[id] -=
        o * (lower_bound(a.begin(), a.end(), make_pair(w, 0ll)) - a.begin());
  }
}
void calc(long long v) {
  I.clear(), O.clear();
  I.push_back({(x - w[v] + MOD) % MOD, v}), O.push_back({0, v});
  for (auto u : g[v])
    if (!mk[u]) {
      dfs(u, v, 1, w[v], 0);
      modif(II, OO, -1);
      for (auto x : II) I.push_back(x);
      for (auto x : OO) O.push_back(x);
      II.clear(), OO.clear();
    }
  modif(I, O, 1);
}
void sol(long long v) {
  mk[v] = 1, calc(v);
  for (auto u : g[v])
    if (!mk[u]) rfs(u, rt = 0, siz[u]), sol(u);
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> MOD >> k >> x;
  powk[0] = invk[0] = 1;
  for (int i = 1, inv = pwm(k, MOD - 2); i < maxn; i++)
    powk[i] = powk[i - 1] * k % MOD, invk[i] = invk[i - 1] * inv % MOD;
  for (int i = 1; i <= n; i++) cin >> w[i];
  for (int i = 1; i < n; i++) {
    long long v, u;
    cin >> v >> u;
    g[v].push_back(u), g[u].push_back(v);
  }
  rfs(1, rt = 0, n);
  sol(rt);
  for (int i = 1; i <= n; i++)
    ans += 2 * (in[i] * (n - in[i]) + out[i] * (n - out[i])) +
           in[i] * (n - out[i]) + (n - in[i]) * out[i];
  cout << n * n * n - ans / 2;
  return 0;
}
