#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1);
pair<int, int> P1[110000], P2[110000];
vector<int> E[110000];
int Pow[110000], size[110000], bo[110000], v[110000], in0[110000], in1[110000],
    out0[110000], out1[110000];
int ni_K, K, X, Y, n, m, tot;
long long ans;
void prep() {
  int x = K, y = Y - 2;
  ni_K = 1;
  while (y) {
    if (y & 1) ni_K = 1ll * ni_K * x % Y;
    x = 1ll * x * x % Y;
    y /= 2;
  }
  Pow[0] = 1;
  for (int i = 1; i <= n; ++i) Pow[i] = 1ll * Pow[i - 1] * K % Y;
}
int getsize(int x, int ff) {
  size[x] = 1;
  for (int i : E[x])
    if (i != ff) size[x] += getsize(i, x);
  return size[x];
}
int getroot(int x) {
  int maxx = 0, id;
  for (int i : E[x])
    if (!bo[i] && size[i] > maxx) maxx = size[i], id = i;
  if (maxx * 2 > size[x]) {
    size[x] -= maxx;
    size[id] += maxx;
    return getroot(id);
  }
  return x;
}
void dfs(int x, int rev, int down, int up, int dep) {
  P1[++tot] = make_pair((1ll * (X - up) * rev % Y + Y) % Y, x);
  P2[tot] = make_pair(down, x);
  bo[x] = 1;
  for (int i : E[x])
    if (!bo[i])
      dfs(i, 1ll * rev * ni_K % Y, (down + 1ll * Pow[dep + 1] * v[i]) % Y,
          (1ll * up * K + v[i]) % Y, dep + 1);
  bo[x] = 0;
}
void calc(int l, int r, int flag) {
  sort(P1 + l, P1 + r + 1);
  sort(P2 + l, P2 + r + 1);
  int i, j = l, i0, j0;
  for (i = l; i <= r; i = i0) {
    for (i0 = i; i0 <= r && P1[i].first == P1[i0].first; ++i0)
      ;
    while (j <= r && P2[j].first < P1[i].first) j++;
    for (j0 = j; j0 <= r && P1[i].first == P2[j0].first; ++j0)
      ;
    if (j != j0)
      for (int k = i; k <= i0 - 1; ++k) out0[P1[k].second] += flag * (j0 - j);
    if (j != j0)
      for (int k = j; k <= j0 - 1; ++k) in0[P2[k].second] += flag * (i0 - i);
    j = j0;
  }
}
void work(int x) {
  x = getroot(x);
  bo[x] = 1;
  tot = 0;
  for (int i : E[x])
    if (!bo[i]) {
      int tt = tot;
      dfs(i, ni_K, (1ll * v[i] * K + v[x]) % Y, v[i], 1);
      calc(tt + 1, tot, -1);
    }
  P1[++tot] = make_pair(X, x);
  P2[tot] = make_pair(v[x], x);
  calc(1, tot, 1);
  for (int i : E[x])
    if (!bo[i]) work(i);
}
int main() {
  int x, y;
  scanf("%d%d%d%d", &n, &Y, &K, &X);
  prep();
  for (int i = 1; i <= n; ++i) scanf("%d", &v[i]);
  for (int i = 1; i <= n - 1; ++i)
    scanf("%d%d", &x, &y), E[x].push_back(y), E[y].push_back(x);
  getsize(1, 0);
  work(1);
  for (int i = 1; i <= n; ++i) in1[i] = n - in0[i], out1[i] = n - out0[i];
  ans = 0;
  for (int i = 1; i <= n; ++i)
    ans += 2ll * out0[i] * out1[i] + 2ll * in0[i] * in1[i] +
           1ll * in0[i] * out1[i] + 1ll * in1[i] * out0[i];
  printf("%I64d\n", 1ll * n * n * n - ans / 2);
}
