#include <bits/stdc++.h>
using namespace std;
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << ' ' << H;
  debug_out(T...);
}
const int INF = 1e9;
const int N = 1e5 + 5;
int n, MOD, BASE, X;
int P[N], I[N], W[N], SZ[N], H[N], DW[N], UP[N];
long long IN[N], OUT[N];
bool M[N];
vector<int> G[N];
map<int, int> CNT, TNC;
int SMul(int a, int b) { return 1LL * a * b % MOD; }
int SAdd(int a, int b) {
  int c = a + b;
  if (c >= MOD) c -= MOD;
  return c;
}
int Power(int a, int b) {
  int res = 1;
  for (; b; b >>= 1, a = SMul(a, a))
    if (b & 1) res = SMul(res, a);
  return res;
}
void DFSZ(int v, int p = 0) {
  SZ[v] = 1;
  for (int u : G[v])
    if (u != p && !M[u]) DFSZ(u, v), SZ[v] += SZ[u];
}
int GetCentroid(int v, int p = 0, int m = 0) {
  if (!p) DFSZ(v), m = SZ[v];
  for (int u : G[v])
    if (u != p && !M[u] && SZ[u] * 2 > m) return GetCentroid(u, v, m);
  return v;
}
void DFS(int v, int p = 0) {
  H[v] = H[p] + 1;
  DW[v] = SAdd(DW[p], SMul(P[H[v] - 1], W[v]));
  UP[v] = SAdd(SMul(UP[p], BASE), W[v]);
  CNT[DW[v]]++, TNC[SMul(SAdd(X, MOD - UP[v]), I[H[v]])]++;
  for (int u : G[v])
    if (u != p && !M[u]) DFS(u, v);
}
void Change(int d, int v, int p = 0) {
  CNT[DW[v]] += d, TNC[SMul(SAdd(X, MOD - UP[v]), I[H[v]])] += d;
  for (int u : G[v])
    if (u != p && !M[u]) Change(d, u, v);
}
void F(int v, int p = 0, int up = 0, int dw = 0) {
  up = SAdd(SMul(up, BASE), W[v]);
  dw = SAdd(dw, SMul(P[H[v] - 2], W[v]));
  IN[v] += TNC[dw], OUT[v] += CNT[SMul(SAdd(X, MOD - up), I[H[v] - 1])];
  for (int u : G[v])
    if (u != p && !M[u]) F(u, v, up, dw);
}
void Decompose(int v) {
  v = GetCentroid(v), DFS(v);
  M[v] = 1;
  for (int u : G[v])
    if (!M[u]) Change(-1, u), F(u), Change(+1, u);
  OUT[v] += CNT[X], IN[v] += TNC[0];
  CNT.clear(), TNC.clear();
  for (int u : G[v])
    if (!M[u]) Decompose(u);
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  mt19937 Rnd(time(0));
  cin >> n >> MOD >> BASE >> X;
  P[0] = 1;
  for (int i = 1; i < N; i++) P[i] = SMul(P[i - 1], BASE);
  I[N - 1] = Power(P[N - 1], MOD - 2);
  for (int i = N - 1; i > 0; i--) I[i - 1] = SMul(I[i], BASE);
  for (int i = 1; i <= n; i++) cin >> W[i];
  for (int i = 1; i < n; i++) {
    int v, u;
    cin >> v >> u;
    G[v].push_back(u), G[u].push_back(v);
  }
  Decompose(1);
  long long p = 0;
  for (int v = 1; v <= n; v++)
    p += 1LL * IN[v] * (n - OUT[v]) + 1LL * (n - IN[v]) * OUT[v] +
         2LL * (1LL * IN[v] * (n - IN[v]) + 1LL * OUT[v] * (n - OUT[v]));
  cout << 1LL * n * n * n - p / 2 << endl;
  return 0;
}
