#include <bits/stdc++.h>
using namespace std;
int n, y, k, x;
const int N = 300000;
int pk[N], ipk[N], vi[N];
int I1[N], O1[N], siz[N], mxc[N], vis[N];
vector<int> bi[N];
vector<int> Mu, Md, Nu[N], Nd[N];
long long res;
int powi(int a, int b) {
  int c = 1;
  for (; b; b >>= 1, a = 1ll * a * a % y)
    if (b & 1) c = 1ll * c * a % y;
  return c;
}
void dfs1(int t, int f) {
  siz[t] = 1;
  mxc[t] = 0;
  for (int i = 0; i < bi[t].size(); ++i)
    if (!vis[bi[t][i]] && bi[t][i] != f) {
      dfs1(bi[t][i], t);
      siz[t] += siz[bi[t][i]];
      mxc[t] = max(mxc[t], siz[bi[t][i]]);
    }
}
void dfs2(int t, int f, int u, int d, int wu, int wd) {
  d++;
  wu = (1ll * wu * k + vi[t]) % y;
  wd = (wd + 1ll * pk[d] * vi[t]) % y;
  int vd = 1ll * (x - wu + y) * ipk[d] % y;
  Mu.push_back(wd), Nu[u].push_back(wd);
  Md.push_back(vd), Nd[u].push_back(vd);
  for (int i = 0; i < bi[t].size(); ++i)
    if (!vis[bi[t][i]] && bi[t][i] != f) dfs2(bi[t][i], t, u, d, wu, wd);
}
void dfs3(int t, int f, int u, int d, int wu, int wd) {
  d++;
  wu = (1ll * wu * k + vi[t]) % y;
  wd = (wd + 1ll * pk[d] * vi[t]) % y;
  int vd = 1ll * (x - wu + y) * ipk[d] % y;
  I1[t] += (upper_bound(Md.begin(), Md.end(), wd) -
            lower_bound(Md.begin(), Md.end(), wd)) -
           (upper_bound(Nd[u].begin(), Nd[u].end(), wd) -
            lower_bound(Nd[u].begin(), Nd[u].end(), wd));
  O1[t] += (upper_bound(Mu.begin(), Mu.end(), vd) -
            lower_bound(Mu.begin(), Mu.end(), vd)) -
           (upper_bound(Nu[u].begin(), Nu[u].end(), vd) -
            lower_bound(Nu[u].begin(), Nu[u].end(), vd));
  for (int i = 0; i < bi[t].size(); ++i)
    if (!vis[bi[t][i]] && bi[t][i] != f) dfs3(bi[t][i], t, u, d, wu, wd);
}
int dfs4(int t, int f, int s) {
  int v = t;
  for (int i = 0; i < bi[t].size(); ++i)
    if (!vis[bi[t][i]] && bi[t][i] != f) {
      int w = dfs4(bi[t][i], t, s);
      if (max(mxc[w], s - siz[w]) < max(mxc[v], s - siz[v])) v = w;
    }
  return v;
}
void solve(int t) {
  vis[t] = 1;
  for (int i = 0; i < bi[t].size(); ++i)
    if (!vis[bi[t][i]]) dfs1(bi[t][i], t);
  Md.clear();
  Mu.clear();
  for (int i = 0; i < bi[t].size(); ++i)
    if (!vis[bi[t][i]]) Nd[bi[t][i]].clear(), Nu[bi[t][i]].clear();
  for (int i = 0; i < bi[t].size(); ++i)
    if (!vis[bi[t][i]]) dfs2(bi[t][i], t, bi[t][i], 0, vi[t], vi[t]);
  Md.push_back(1ll * (x - vi[t] + y) * ipk[0] % y);
  Mu.push_back(vi[t]);
  sort(Md.begin(), Md.end()), sort(Mu.begin(), Mu.end());
  for (int i = 0; i < bi[t].size(); ++i)
    if (!vis[bi[t][i]])
      sort(Nd[bi[t][i]].begin(), Nd[bi[t][i]].end()),
          sort(Nu[bi[t][i]].begin(), Nu[bi[t][i]].end());
  for (int i = 0; i < bi[t].size(); ++i)
    if (!vis[bi[t][i]]) dfs3(bi[t][i], t, bi[t][i], 0, 0, 0);
  I1[t] += (upper_bound(Md.begin(), Md.end(), 0) -
            lower_bound(Md.begin(), Md.end(), 0));
  O1[t] += (upper_bound(Mu.begin(), Mu.end(), x) -
            lower_bound(Mu.begin(), Mu.end(), x));
  for (int i = 0; i < bi[t].size(); ++i)
    if (!vis[bi[t][i]]) solve(dfs4(bi[t][i], t, siz[bi[t][i]]));
}
int main() {
  ios ::sync_with_stdio(0);
  cin >> n >> y >> k >> x;
  ipk[0] = pk[0] = 1;
  for (int i = 1; i <= n; ++i) pk[i] = 1ll * pk[i - 1] * k % y;
  ipk[1] = powi(k, y - 2);
  for (int i = 2; i <= n; ++i) ipk[i] = 1ll * ipk[i - 1] * ipk[1] % y;
  for (int i = 1; i <= n; ++i) cin >> vi[i];
  for (int i = 1; i < n; ++i) {
    int a, b;
    cin >> a >> b;
    bi[a].push_back(b);
    bi[b].push_back(a);
  }
  solve((dfs1(1, 0), dfs4(1, 0, n)));
  res = 2ll * n * n * n;
  for (int i = 1; i <= n; ++i)
    res -= 2ll * O1[i] * (n - O1[i]) + 2ll * I1[i] * (n - I1[i]) +
           1ll * O1[i] * (n - I1[i]) + 1ll * (n - O1[i]) * I1[i];
  cout << res / 2 << endl;
}
