#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 9;
int mod, X, inv_k, k;
int in1[N], out1[N], in0[N], out0[N], pw[N], a[N];
int centroid;
long long quick(long long a, long long b) {
  long long res = 1;
  while (b) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
    b >>= 1;
  }
  return res;
}
vector<int> g[N];
bool vis[N];
int sz[N], dep[N];
long long tot;
inline void dfs(int u, int f, int n) {
  int ok = 1;
  sz[u] = 1;
  for (auto v : g[u]) {
    if (v == f || vis[v]) continue;
    dfs(v, u, n);
    if (sz[v] * 2 > n) ok = 0;
    sz[u] += sz[v];
  }
  if (2 * (n - sz[u]) > n) ok = 0;
  if (ok) centroid = u;
}
int cnt;
pair<int, int> P1[N], P2[N];
inline void dfs2(const int &u, const int &t, const int &pre, const int &suf,
                 const int &depth) {
  vis[u] = 1;
  dep[u] = depth;
  P2[cnt] = make_pair(1ll * (X - suf + mod) * t % mod, u);
  P1[cnt++] = make_pair(pre, u);
  for (auto v : g[u]) {
    if (vis[v]) continue;
    dfs2(v, 1ll * t * inv_k % mod, (pre + 1ll * a[v] * pw[depth + 1]) % mod,
         (1ll * suf * k + a[v]) % mod, depth + 1);
  }
  vis[u] = 0;
}
inline void cal(const int &l, const int &r, const int &op) {
  sort(P1 + l, P1 + r);
  sort(P2 + l, P2 + r);
  int i = l, i0 = l, j = l, j0 = l;
  for (; i < r; i = i0, j = j0) {
    while (i0 < r && P1[i0].first == P1[i].first) ++i0;
    while (j < r && P2[j].first < P1[i].first) ++j;
    j0 = j;
    while (j0 < r && P2[j0].first == P1[i].first) ++j0;
    for (int k = (j); k < (j0); ++k) in1[P2[k].second] += op * (i0 - i);
    for (int k = (i); k < (i0); ++k) out1[P1[k].second] += op * (j0 - j);
  }
}
inline void doit(int x, int n) {
  dfs(x, 0, n);
  x = centroid;
  dep[x] = 0;
  cnt = 0;
  vis[x] = 1;
  for (auto v : g[x]) {
    if (vis[v]) continue;
    int l = cnt;
    dfs2(v, inv_k, (a[x] + 1ll * k * a[v]) % mod, a[v], 1);
    cal(l, cnt, -1);
  }
  P1[cnt] = make_pair(a[x], x);
  P2[cnt++] = make_pair(X, x);
  cal(0, cnt, 1);
  for (auto v : g[x]) {
    if (vis[v]) continue;
    doit(v, sz[v] < sz[x] ? sz[v] : n - sz[x]);
  }
}
int main() {
  int n;
  scanf("%d%d%d%d", &n, &mod, &k, &X);
  pw[0] = 1;
  for (int i = (1); i < (N); ++i) pw[i] = 1ll * pw[i - 1] * k % mod;
  inv_k = quick(k, mod - 2);
  for (int i = (1); i < (n + 1); ++i) scanf("%d", &a[i]);
  for (int i = (1); i < (n); ++i) {
    int u, v;
    scanf("%d%d", &u, &v);
    g[u].push_back(v);
    g[v].push_back(u);
  }
  doit(1, n);
  long long ans = 0;
  for (int i = (1); i < (n + 1); ++i)
    in0[i] = n - in1[i], out0[i] = n - out1[i];
  for (int i = (1); i < (n + 1); ++i)
    ans += 2ll * in0[i] * in1[i] + 2ll * out0[i] * out1[i] +
           1ll * in0[i] * out1[i] + 1ll * in1[i] * out0[i];
  ans = 1ll * n * n * n - ans / 2;
  cout << ans;
}
