#include <bits/stdc++.h>
using namespace std;
int i, j, k, n, m, K, X, mo;
int val[100010], g[100010], gg[100010], fa[100010], F[100010], h[100010],
    dep[100010], ha[100010], v[100010], in[100010], chu[100010];
vector<int> a[100010];
long long an;
map<int, int> Ma, ge;
inline int getroot(int x) {
  int l, r, i, mi = 100010, id = 0;
  g[l = r = 1] = x;
  fa[x] = 0;
  for (; l <= r; l++) {
    int A = g[l];
    for (i = 0; i < a[A].size(); i++) {
      int B = a[A][i];
      if (B == fa[A] || F[B]) continue;
      g[++r] = B;
      fa[B] = A;
    }
  }
  for (l--; l; l--) {
    int A = g[l], ma = 0;
    gg[A] = 1;
    for (i = 0; i < a[A].size(); i++) {
      int B = a[A][i];
      if (B == fa[A] || F[B]) continue;
      ma = max(ma, gg[B]);
      gg[A] += gg[B];
    }
    ma = max(ma, r - gg[A]);
    if (ma < mi) mi = ma, id = A;
  }
  return id;
}
void work(int x, int Fa, int Gg, int Dep, int H, int _) {
  int l, r, i;
  g[l = r = 1] = x;
  fa[x] = Fa;
  gg[x] = Gg;
  dep[x] = Dep;
  h[x] = H;
  Ma.clear();
  ge.clear();
  for (; l <= r; l++) {
    int A = g[l];
    {
      int zhi = 1ll * (X - h[A]) * v[dep[A]] % mo;
      if (zhi < 0) zhi += mo;
      Ma[zhi]++;
      ge[gg[A]]++;
    }
    for (i = 0; i < a[A].size(); i++) {
      int B = a[A][i];
      if (B == fa[A] || F[B]) continue;
      g[++r] = B;
      fa[B] = A;
      dep[B] = dep[A] + 1;
      gg[B] = (1ll * ha[dep[B]] * val[B] + gg[A]) % mo;
      h[B] = (1ll * h[A] * K + val[B]) % mo;
    }
  }
  for (l--; l; l--) {
    int A = g[l];
    chu[A] += Ma[gg[A]] * _;
    int zhi = 1ll * (X - h[A]) * v[dep[A]] % mo;
    if (zhi < 0) zhi += mo;
    in[A] += ge[zhi] * _;
  }
}
void dfs(int x) {
  int A = getroot(x), i;
  F[A] = 1;
  work(A, 0, val[A], 0, 0, 1);
  for (i = 0; i < a[A].size(); i++) {
    int B = a[A][i];
    if (F[B]) continue;
    work(B, A, (1ll * val[B] * K + val[A]) % mo, 1, val[B], -1);
  }
  for (i = 0; i < a[A].size(); i++) {
    int B = a[A][i];
    if (F[B]) continue;
    dfs(B);
  }
}
inline long long pow(long long x, long long y) {
  long long an = 1;
  for (; y; y >>= 1) {
    if (y & 1) an = an * x % mo;
    x = x * x % mo;
  }
  return an;
}
int main() {
  scanf("%d%d%d%d", &n, &mo, &K, &X);
  ha[0] = 1;
  for (i = 1; i <= n; i++) ha[i] = 1ll * ha[i - 1] * K % mo;
  for (i = 0; i <= n; i++) v[i] = pow(ha[i], mo - 2);
  for (i = 1; i <= n; i++) scanf("%d", &val[i]);
  for (i = 1; i <= n - 1; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    a[x].push_back(y);
    a[y].push_back(x);
  }
  dfs(1);
  for (i = 1; i <= n; i++) {
    long long A = chu[i], B = in[i];
    an += A * A + (n - A) * (n - A) + B * B + (n - B) * (n - B) + A * B +
          (n - A) * (n - B);
  }
  an -= 1ll * n * n * n;
  printf("%I64d\n", an / 2);
  return 0;
}
