#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const int inf = 2e9;
int gi() {
  int x = 0, o = 1;
  char ch = getchar();
  while ((ch < '0' || ch > '9') && ch != '-') ch = getchar();
  if (ch == '-') o = -1, ch = getchar();
  while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
  return x * o;
}
int qpow(int a, int b, int mod) {
  int ret = 1;
  while (b) {
    if (b & 1) ret = 1ll * ret * a % mod;
    a = 1ll * a * a % mod;
    b >>= 1;
  }
  return ret;
}
struct edge {
  int to, nxt;
} e[N << 1];
int tot = 0, h[N];
void adde(int u, int v) {
  e[++tot] = (edge){v, h[u]};
  h[u] = tot;
  e[++tot] = (edge){u, h[v]};
  h[v] = tot;
}
int n, y, k, x, a[N];
int sz = 0, rt = 0, siz[N], f[N];
int in1[N], out1[N], in0[N], out0[N], pw[N], ipw[N];
bool vis[N];
void getrt(int u, int fa) {
  siz[u] = 1;
  f[u] = 0;
  for (int i = h[u]; i; i = e[i].nxt) {
    int v = e[i].to;
    if (v == fa || vis[v]) continue;
    getrt(v, u);
    siz[u] += siz[v];
    f[u] = max(f[u], siz[v]);
  }
  f[u] = max(f[u], sz - siz[u]);
  if (f[u] < f[rt]) rt = u;
}
struct node {
  int val, id;
} A[N], B[N];
bool operator<(node a, node b) { return a.val < b.val; }
int cnt = 0;
void dfs(int u, int fa, int dep, int s1, int s2) {
  s1 = (1ll * s1 * k + a[u]) % y;
  s2 = (s2 + 1ll * (dep ? pw[dep - 1] : 0) * a[u] % y) % y;
  A[++cnt] = {int(1ll * (x - s1 + y) * ipw[dep + 1] % y), u};
  B[cnt] = {s2, u};
  for (int i = h[u]; i; i = e[i].nxt) {
    int v = e[i].to;
    if (v == fa || vis[v]) continue;
    dfs(v, u, dep + 1, s1, s2);
  }
}
int binary(node *A, int x) {
  int l = 0, r = cnt + 1, L = 0, R = 0;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (A[mid].val < x)
      L = mid, l = mid + 1;
    else
      r = mid - 1;
  }
  l = 0, r = cnt + 1;
  while (l <= r) {
    int mid = (l + r) >> 1;
    if (A[mid].val > x)
      R = mid, r = mid - 1;
    else
      l = mid + 1;
  }
  L++;
  R--;
  if (L > R)
    return 0;
  else
    return R - L + 1;
}
void calc(int u, int op, int dep, int s1) {
  cnt = 0;
  dfs(u, 0, dep, s1, 0);
  sort(A + 1, A + cnt + 1);
  sort(B + 1, B + cnt + 1);
  A[0] = {-inf, 0};
  A[cnt + 1] = {inf, 0};
  B[0] = {-inf, 0};
  B[cnt + 1] = {inf, 0};
  for (int i = 1; i <= cnt; i++) out1[A[i].id] += op * binary(B, A[i].val);
  for (int i = 1; i <= cnt; i++) in1[B[i].id] += op * binary(A, B[i].val);
}
void solve(int u) {
  vis[u] = 1;
  calc(u, 1, 0, 0);
  for (int i = h[u]; i; i = e[i].nxt) {
    int v = e[i].to;
    if (vis[v]) continue;
    calc(v, -1, 1, a[u]);
  }
  for (int i = h[u]; i; i = e[i].nxt) {
    int v = e[i].to;
    if (vis[v]) continue;
    getrt(v, u);
    sz = siz[v];
    rt = 0;
    getrt(v, u);
    solve(rt);
  }
}
int main() {
  cin >> n >> y >> k >> x;
  pw[0] = 1;
  for (int i = 1; i <= n; i++) pw[i] = 1ll * pw[i - 1] * k % y;
  ipw[0] = 1;
  for (int i = 1; i <= n; i++)
    ipw[i] = 1ll * ipw[i - 1] * qpow(k, y - 2, y) % y;
  for (int i = 1; i <= n; i++) a[i] = gi();
  for (int i = 1, u, v; i < n; i++) u = gi(), v = gi(), adde(u, v);
  sz = f[0] = n;
  getrt(1, 0);
  solve(rt);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    in0[i] = n - in1[i];
    out0[i] = n - out1[i];
    ans += 1ll * in0[i] * out1[i] + 1ll * in1[i] * out0[i] +
           2ll * in0[i] * in1[i] + 2ll * out0[i] * out1[i];
  }
  printf("%lld\n", 1ll * n * n * n - ans / 2);
  return 0;
}
