#include <bits/stdc++.h>
using namespace std;
struct node {
  int t, n;
} e[110000 << 1];
int vi[110000], fi[110000], pi[110000], in0[110000], out0[110000], qi[110000];
int d1[110000], d2[110000], sz[110000], pre[110000], dep[110000], q[110000];
bool used[110000], can[110000];
int n, p, nk, m, tot, vnk;
struct Thas {
  int fi[1999973], id[1999973], ha[110000], sz[110000], ne[110000], cnt;
  inline void clear(void) { cnt = 0; }
  void ins(int x) {
    if (x < 0) x += p;
    int t = x % 1999973;
    if (id[t] != tot) id[t] = tot, fi[t] = -1;
    for (int i = fi[t]; i != -1; i = ne[i])
      if (ha[i] == x) {
        sz[i]++;
        return;
      }
    ha[++cnt] = x, sz[cnt] = 1, ne[cnt] = fi[t], fi[t] = cnt;
  }
  int get(int x) {
    if (x < 0) x += p;
    int t = x % 1999973;
    if (id[t] == tot)
      for (int i = fi[t]; i != -1; i = ne[i])
        if (ha[i] == x) return sz[i];
    return 0;
  }
} h1, h2;
inline int mpow(int x, int y, int t = 1) {
  for (; y; y >>= 1, x = x * 1LL * x % p)
    if (y & 1) t = t * 1LL * x % p;
  return t;
}
inline int inv(int x) { return mpow(x, p - 2); }
inline void insert(int x, int y) { e[++tot] = (node){y, fi[x]}, fi[x] = tot; }
int getrt(int x, int t = 1) {
  pre[q[1] = x] = -1;
  for (int h = 1, tt; h <= t; h++) {
    can[x = q[h]] = true, sz[x] = 1;
    for (int i = fi[x]; i != -1; i = e[i].n)
      if ((tt = e[i].t) != pre[x] && !used[e[i].t]) pre[q[++t] = tt] = x;
  }
  for (int i = t; i >= 1; i--) {
    x = q[i], sz[pre[x]] += sz[x];
    if (sz[x] * 2 > t) can[pre[x]] = false;
    if (sz[x] * 2 >= t && can[x]) return x;
  }
}
void BFS(int x, int t = 1) {
  pre[q[1] = x] = -1, d1[x] = vi[x], d2[x] = 0, dep[x] = 0;
  for (int h = 1, tt; h <= t; h++)
    for (int i = fi[x = q[h]]; i != -1; i = e[i].n)
      if ((tt = e[i].t) != pre[x] && !used[e[i].t])
        pre[q[++t] = tt] = x, d2[tt] = (d2[x] * 1LL * nk + vi[tt]) % p,
                     d1[tt] =
                         (d1[x] + vi[tt] * 1LL * pi[dep[tt] = dep[x] + 1]) % p;
  for (int i = 1; i <= t; i++) {
    if (d1[x = q[i]] == m) out0[q[1]]++, in0[x]++;
    if (i > 1 && (d2[x] + vi[q[1]] * 1LL * pi[dep[x]]) % p == m)
      in0[q[1]]++, out0[x]++;
  }
}
void solve(int x, int t = 1) {
  q[1] = x;
  for (int h = 1; h <= t; h++)
    for (int i = fi[x = q[h]]; i != -1; i = e[i].n)
      if (e[i].t != pre[x] && !used[e[i].t]) q[++t] = e[i].t;
  for (int i = 1; i <= t; i++)
    in0[q[i]] += h2.get(d1[q[i]]),
        out0[q[i]] += h1.get((m - d2[q[i]] + p) * 1LL * qi[dep[q[i]]] % p);
  for (int i = 1; i <= t; i++)
    h2.ins((m - d2[q[i]] + p) * 1LL * qi[dep[q[i]]] % p), h1.ins(d1[q[i]]);
}
int ch[110000];
void DFS(int x) {
  used[x] = true, BFS(x);
  int cs = 0;
  for (int i = fi[x]; i != -1; i = e[i].n)
    if (!used[e[i].t]) ch[++cs] = e[i].t;
  tot++, h1.clear(), h2.clear();
  for (int i = 1; i <= cs; i++) solve(ch[i]);
  tot++, h1.clear(), h2.clear();
  for (int i = cs; i >= 1; i--) solve(ch[i]);
  for (int i = fi[x]; i != -1; i = e[i].n)
    if (!used[e[i].t]) DFS(getrt(e[i].t));
}
int main(void) {
  scanf("%d%d%d%d", &n, &p, &nk, &m), pi[0] = qi[0] = 1, vnk = inv(nk), m %= p;
  for (int i = 1; i <= n + 1; i++)
    pi[i] = pi[i - 1] * 1LL * nk % p, qi[i] = qi[i - 1] * 1LL * vnk % p;
  fill(fi, fi + n + 1, -1), tot = 1;
  for (int i = 1; i <= n; i++) scanf("%d", vi + i);
  for (int i = 1; i <= n - 1; i++) {
    int x, y;
    scanf("%d%d", &x, &y), insert(x, y), insert(y, x);
  }
  tot = 0, DFS(getrt(1));
  long long ans = 0;
  for (int i = 1; i <= n; i++)
    ans += (in0[i] + out0[i]) * 1LL * (2 * n - out0[i] - in0[i]) +
           in0[i] * 1LL * (n - in0[i]) + out0[i] * 1LL * (n - out0[i]);
  ans = n * 1LL * n * n - (ans >> 1);
  cout << ans << endl;
  return 0;
}
