#include <bits/stdc++.h>
using namespace std;
vector<int> a[100005];
long long ans, tmp;
pair<int, int> A[100005], B[100005];
int Pow[100005], Rev[100005], son[100005], val[100005], vis[100005], ma[100005],
    OUT[100005], IN[100005];
int sum, P, n, i, x, y, M, tot, X;
inline int Q_pow(int a, int b) {
  int res = 1;
  for (; b; b >>= 1, a = a * 1ll * a % P)
    if (b & 1) res = res * 1ll * a % P;
  return res;
}
inline int get_root(int k, int fa) {
  son[k] = 1;
  int res = 0;
  for (int i = 0, go; i < a[k].size(); i++) {
    if (vis[go = a[k][i]] || go == fa) continue;
    int tmp = get_root(go, k);
    son[k] += son[go];
    ma[k] = max(ma[k], son[go]);
    if (ma[tmp] < ma[res] || !res) res = tmp;
  }
  ma[k] = max(ma[k], sum - son[k]);
  if (ma[k] < ma[res] || !res) res = k;
  return res;
}
inline void dfs(int k, int out, int in, int fa, int deep) {
  out = (out * 1ll * M % P + val[k]) % P;
  in = (in + Pow[deep] * 1ll * val[k] % P) % P;
  A[++tot] = make_pair((X - out + P) * 1ll * Rev[deep] % P, k);
  B[tot] = make_pair(in, k);
  for (int i = 0, go; i < a[k].size(); i++) {
    if (vis[go = a[k][i]] || go == fa) continue;
    dfs(go, out, in, k, deep + 1);
  }
}
inline void calc(int L, int R, int m) {
  sort(A + L, A + R + 1);
  sort(B + L, B + R + 1);
  int j = L, p, q;
  for (int i = L; i <= R;) {
    for (p = i; p <= R && A[p].first == A[i].first; p++)
      ;
    for (; j <= R && B[j].first < A[i].first; j++)
      ;
    for (q = j; q <= R && B[q].first == A[i].first; q++)
      ;
    for (int k = i; k < p; k++) OUT[A[k].second] += m * (q - j);
    for (int k = j; k < q; k++) IN[B[k].second] += m * (p - i);
    i = p;
    j = q;
  }
}
inline void divide(int k) {
  vis[k] = 1;
  tot = 0;
  for (int i = 0, go; i < a[k].size(); i++) {
    if (vis[go = a[k][i]]) continue;
    int tmp = tot;
    dfs(go, val[k], 0, k, 1);
    calc(tmp + 1, tot, -1);
  }
  A[++tot] = make_pair((X - val[k] + P) % P, k);
  B[tot] = make_pair(0, k);
  calc(1, tot, 1);
  for (int i = 0, go; i < a[k].size(); i++)
    if (!vis[go = a[k][i]]) sum = son[go], divide(get_root(go, -1));
}
int main() {
  scanf("%d%d%d%d", &n, &P, &M, &X);
  for (i = 1; i <= n; i++) scanf("%d", &val[i]);
  for (i = 1; i < n; i++)
    scanf("%d%d", &x, &y), a[x].push_back(y), a[y].push_back(x);
  for (i = Pow[0] = 1; i <= n; i++) Pow[i] = Pow[i - 1] * 1ll * M % P;
  for (i = Rev[0] = 1; i <= n; i++) Rev[i] = Q_pow(Pow[i], P - 2);
  sum = n;
  divide(get_root(1, -1));
  ans = n * 1ll * n * 1ll * n;
  for (i = 1; i <= n; i++) {
    tmp += 2ll * OUT[i] * 1ll * (n - OUT[i]);
    tmp += 2ll * IN[i] * 1ll * (n - IN[i]);
    tmp += IN[i] * 1ll * (n - OUT[i]);
    tmp += OUT[i] * 1ll * (n - IN[i]);
  }
  ans -= tmp / 2ll;
  printf("%I64d\n", ans);
}
