#include <bits/stdc++.h>
using namespace std;
long long read() {
  char ch = getchar();
  long long x = 0;
  int op = 1;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') op = -1;
  for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - '0';
  return x * op;
}
int n, mod, mi, k, val, cnt, rt, sum, tot, w[100005], head[100005], pwk[100005],
    ivk[100005], siz[100005], in1[100005], out1[100005];
bool vis[100005];
pair<int, int> p[100005], q[100005];
struct edge {
  int to, nxt;
} e[100005 << 1];
int ksm(int x, int p) {
  int ret = 1;
  for (; p; p >>= 1, x = (long long)x * x % mod)
    if (p & 1) ret = (long long)ret * x % mod;
  return ret;
}
void adde(int x, int y) {
  e[++cnt].to = y;
  e[cnt].nxt = head[x];
  head[x] = cnt;
}
void ins(int x, int y) {
  adde(x, y);
  adde(y, x);
}
void getrt(int u, int pr) {
  siz[u] = 1;
  int tmp = 0;
  for (int i = head[u]; i; i = e[i].nxt)
    if (!vis[e[i].to] && e[i].to != pr) {
      int v = e[i].to;
      getrt(v, u);
      siz[u] += siz[v];
      tmp = max(tmp, siz[v]);
    }
  tmp = max(tmp, sum - siz[u]);
  if (tmp < mi) mi = tmp, rt = u;
}
void dfs(int u, int pr, int dep, int val1, int val2) {
  val1 = ((long long)val1 * k + w[u]) % mod,
  val2 = (val2 + (long long)w[u] * (!dep ? 0 : pwk[dep - 1])) % mod, dep++;
  p[++tot] = pair<int, int>((long long)(val + mod - val1) * ivk[dep] % mod, u),
  q[tot] = pair<int, int>(val2, u);
  for (int i = head[u]; i; i = e[i].nxt)
    if (!vis[e[i].to] && e[i].to != pr) dfs(e[i].to, u, dep, val1, val2);
}
void cal(int u, int op, int dep, int val1, int val2) {
  tot = 0;
  val1 = ((long long)val1 * k + w[u]) % mod,
  val2 = (val2 + (long long)w[u] * ((!dep) ? 0 : pwk[dep - 1])) % mod, dep++;
  p[++tot] = pair<int, int>((long long)(val + mod - val1) * ivk[dep] % mod, u),
  q[tot] = pair<int, int>(val2, u);
  for (int i = head[u]; i; i = e[i].nxt)
    if (!vis[e[i].to]) dfs(e[i].to, u, dep, val1, val2);
  sort(&p[1], &p[tot + 1]);
  sort(&q[1], &q[tot + 1]);
  int now = 0;
  for (int i = 1, j = 1; i <= tot; i++) {
    for (; j <= tot && q[j].first <= p[i].first; j++) {
      if (j == 1 || q[j].first != q[j - 1].first) now = 0;
      now++;
    }
    if (j != 1 && q[j - 1].first == p[i].first) out1[p[i].second] += op * now;
  }
  now = 0;
  for (int i = 1, j = 1; i <= tot; i++) {
    for (; j <= tot && p[j].first <= q[i].first; j++) {
      if (j == 1 || p[j].first != p[j - 1].first) now = 0;
      now++;
    }
    if (j != 1 && p[j - 1].first == q[i].first) in1[q[i].second] += op * now;
  }
}
void divide(int u) {
  vis[u] = 1;
  cal(u, 1, 0, 0, 0);
  for (int i = head[u]; i; i = e[i].nxt)
    if (!vis[e[i].to]) {
      int v = e[i].to;
      cal(v, -1, 1, w[u], 0);
      rt = 0, sum = siz[v], mi = 1000000001;
      getrt(v, u);
      divide(rt);
    }
}
int main() {
  n = read(), mod = read(), k = read(), val = read();
  int kinv = ksm(k, mod - 2);
  pwk[0] = 1;
  for (int i = (1); i <= (n); i++) pwk[i] = (long long)pwk[i - 1] * k % mod;
  ivk[0] = 1;
  for (int i = (1); i <= (n); i++) ivk[i] = (long long)ivk[i - 1] * kinv % mod;
  for (int i = (1); i <= (n); i++) w[i] = read();
  for (int i = (1); i <= (n - 1); i++) ins(read(), read());
  rt = 0, sum = n, mi = 1000000001;
  getrt(1, 0);
  divide(rt);
  long long ans = 0;
  for (int i = (1); i <= (n); i++) cerr << in1[i] << ' ' << out1[i] << '\n';
  for (int i = (1); i <= (n); i++) {
    int f1 = in1[i], f0 = n - in1[i], g1 = out1[i], g0 = n - out1[i];
    ans +=
        2ll * f1 * f0 + 2ll * g1 * g0 + (long long)f1 * g0 + (long long)f0 * g1;
  }
  cout << (long long)n * n * n - ans / 2 << '\n';
  return 0;
}
