#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
vector<int> g[100100];
long long val[100100];
long long pw[100100];
long long out[100100], in[100100];
long long size[100100];
int fa[100100], dep[100100];
pair<long long, long long> p1[100100], p2[100100];
int top;
bool centroid[100100];
int n;
long long mod, K, x, iK;
long long Pow(long long x, long long y) {
  if (y == 0) return 1;
  long long res = Pow(x, y >> 1);
  res = res * res % mod;
  if (y & 1) res = res * x % mod;
  return res;
}
long long Inv(long long x) { return Pow(x, mod - 2); }
void dfs1(int v, int p) {
  size[v] = 1;
  fa[v] = p;
  if (p)
    dep[v] = dep[p] + 1;
  else
    dep[v] = 1;
  for (int i = 0; i < g[v].size(); i++) {
    int u = g[v][i];
    if (u == p || centroid[u]) continue;
    dfs1(u, v);
    size[v] += size[u];
  }
}
pair<long long, long long> dfs2(int v, int p, int tot) {
  pair<long long, long long> res = make_pair(1e9, 0);
  long long sum = 1, mx = 0;
  for (int i = 0; i < g[v].size(); i++) {
    int u = g[v][i];
    if (u == p || centroid[u]) continue;
    res = min(res, dfs2(u, v, tot));
    sum += size[u];
    mx = max(mx, size[u]);
  }
  mx = max(mx, tot - sum);
  res = min(res, make_pair(mx, 1ll * v));
  return res;
}
void dfs3(int v, int fa, long long inv, long long down, long long up, int dep) {
  top++;
  p1[top] = make_pair(((x - up) * inv % mod + mod) % mod, v);
  p2[top] = make_pair(down, v);
  for (int i = 0; i < g[v].size(); i++) {
    int u = g[v][i];
    if (u == fa || centroid[u]) continue;
    dfs3(u, v, inv * iK % mod, (down + pw[dep + 1] * val[u]) % mod,
         (up * K + val[u]) % mod, dep + 1);
  }
}
void calc(int l, int r, int c) {
  sort(p1 + l, p1 + r + 1);
  sort(p2 + l, p2 + r + 1);
  for (int i = l, j = l, ii, jj; i <= r; i = ii, j = jj) {
    for (ii = i; ii <= r && p1[i].first == p1[ii].first; ii++)
      ;
    for (; j <= r && p2[j].first < p1[i].first; j++)
      ;
    for (jj = j; jj <= r && p1[i].first == p2[jj].first; jj++)
      ;
    if (j != jj) {
      for (int k = i; k < ii; k++) out[p1[k].second] += c * (jj - j);
      for (int k = j; k < jj; k++) in[p2[k].second] += c * (ii - i);
    }
  }
}
void solve(int v) {
  dfs1(v, 0);
  v = dfs2(v, 0, size[v]).second;
  centroid[v] = true;
  top = 0;
  for (int i = 0; i < g[v].size(); i++) {
    int u = g[v][i];
    if (centroid[u]) continue;
    int tt = top;
    dfs3(u, 0, iK, (val[u] * K + val[v]) % mod, val[u], 1);
    calc(tt + 1, top, -1);
  }
  top++;
  p1[top] = make_pair(x, v);
  p2[top] = make_pair(val[v], v);
  calc(1, top, 1);
  for (int i = 0; i < g[v].size(); i++) {
    int u = g[v][i];
    if (centroid[u]) continue;
    solve(u);
  }
}
int main() {
  n = read();
  mod = read();
  K = read();
  x = read();
  iK = Inv(K);
  pw[0] = 1;
  for (int i = 1; i <= n; i++) pw[i] = pw[i - 1] * K % mod;
  for (int i = 1; i <= n; i++) val[i] = read();
  for (int i = 1; i < n; i++) {
    int u = read(), v = read();
    g[u].push_back(v);
    g[v].push_back(u);
  }
  solve(1);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    ans += 2ll * out[i] * (n - out[i]);
    ans += 2ll * in[i] * (n - in[i]);
    ans += in[i] * (n - out[i]);
    ans += (n - in[i]) * out[i];
  }
  ans = 1ll * n * n * n - ans / 2;
  printf("%I64d\n", ans);
  return 0;
}
