#include <bits/stdc++.h>
using namespace std;
typedef int Arr[100005];
long long an;
int N, p, K, m, invk, tot;
Arr powk, sz, val, out, in;
bool bo[100005];
vector<int> Ed[100005];
pair<int, int> gc[100005], gv[100005];
inline int IN() {
  char c;
  register int first = 0;
  while ((c = getchar()) < 48 || c > 57)
    ;
  for (; c > 47 && c < 58; (c = getchar())) first = first * 10 + c - 48;
  return first;
}
inline void Link(int u, int v) {
  Ed[u].push_back(v);
  Ed[v].push_back(u);
}
inline int Pow(int first, int second) {
  int z = 1;
  for (; second; second >>= 1, first = 1ll * first * first % p)
    if (second & 1) z = 1ll * z * first % p;
  return z;
}
void gsz(int u, int ff) {
  sz[u] = 1;
  for (int v : Ed[u])
    if (v ^ ff) {
      gsz(v, u);
      sz[u] += sz[v];
    }
}
int grt(int u) {
  int maxx = 0, w;
  for (int v : Ed[u])
    if (!bo[v] && sz[v] > maxx) maxx = sz[v], w = v;
  if (maxx * 2 > sz[u]) {
    sz[u] -= maxx;
    sz[w] += sz[u];
    return grt(w);
  }
  return u;
}
void dfs(int u, int cvg, int dvg, int Inv, int dep) {
  gc[++tot] = {(1ll * (m - cvg) * Inv % p + p) % p, u};
  gv[tot] = {dvg, u};
  ++dep;
  bo[u] = 1;
  for (int v : Ed[u])
    if (!bo[v])
      dfs(v, (1ll * cvg * K + val[v]) % p, (dvg + 1ll * val[v] * powk[dep]) % p,
          1ll * Inv * invk % p, dep);
  bo[u] = 0;
}
inline void calc(int l, int flag) {
  int r = tot, i, j, ii, jj;
  sort(gc + l, gc + r + 1);
  sort(gv + l, gv + r + 1);
  for (i = ii = j = jj = l; i <= r && j <= r; i = ii, j = jj) {
    if (gc[i].first > gv[j].first) {
      for (jj = j; gv[jj].first == gv[j].first; ++jj)
        ;
      continue;
    }
    if (gc[i].first < gv[j].first) {
      for (ii = i; gc[ii].first == gc[i].first; ++ii)
        ;
      continue;
    }
    for (ii = i; ii <= r && gc[ii].first == gc[i].first; ++ii)
      ;
    for (jj = j; jj <= r && gv[jj].first == gv[j].first; ++jj)
      ;
    for (int _r = ii, z = i; z < _r; ++z) out[gc[z].second] += flag * (jj - j);
    for (int _r = jj, z = j; z < _r; ++z) in[gv[z].second] += flag * (ii - i);
  }
}
void dc(int u) {
  u = grt(u);
  bo[u] = 1;
  tot = 0;
  for (int v : Ed[u])
    if (!bo[v]) {
      int st = tot + 1;
      dfs(v, val[v], (val[u] + 1ll * val[v] * K) % p, invk, 1);
      calc(st, -1);
    }
  gc[++tot] = {m, u};
  gv[tot] = {val[u], u};
  calc(1, 1);
  for (int v : Ed[u])
    if (!bo[v]) dc(v);
}
int main() {
  N = IN();
  p = IN();
  K = IN();
  m = IN();
  invk = Pow(K, p - 2);
  for (int _r = N, i = 1; i <= _r; ++i) val[i] = IN();
  for (int _r = N, i = 1; i < _r; ++i) Link(IN(), IN());
  *powk = 1;
  for (int _r = N, i = 1; i <= _r; ++i) powk[i] = 1ll * powk[i - 1] * K % p;
  gsz(1, 0);
  dc(1);
  for (int _r = N, i = 1; i <= _r; ++i) {
    long long In = N - in[i], Ot = N - out[i];
    an += in[i] * In * 2 + out[i] * Ot * 2 + in[i] * Ot + In * out[i];
  }
  printf("%I64d\n", 1ll * N * N * N - an / 2);
}
