#include using namespace std; using ll = long long; using ull = unsigned long long; using pii = pair; using pll = pair; using vi = vector; using vl = vector; using _loop_int = int; #define REP(i,n) for(_loop_int i=0; i<(_loop_int)(n); i++) #define FOR(i,a,b) for(_loop_int i=(_loop_int)(a); i<(_loop_int)(b); i++) #define FORR(i,a,b) for(_loop_int i=(_loop_int)(b)-1; i>=(_loop_int)(a); i--) #define CHMIN(a,b) (a)=min((a),(b)) #define CHMAX(a,b) (a)=max((a),(b)) #define ALL(v) (v).begin(),(v).end() #define DEBUG(x) cerr<<#x<<": "<<(x)< && is_signed_v, nullptr_t> = nullptr> constexpr mint(T v){ v = v % MOD; val = unsigned(v < 0 ? (v + MOD) : v); } template && is_unsigned_v, nullptr_t> = nullptr> constexpr mint(T v):val(unsigned(v % MOD)){} // raw template, nullptr_t> = nullptr> static constexpr mint raw(T v) { mint x; x.val = unsigned(v); return x; } // operator constexpr mint& operator++() { if (++val == MOD) {val = 0;} return *this; } constexpr mint& operator--() { if (val-- == 0u) {val = MOD - 1;} return *this; } constexpr mint operator++(int) { mint ret = *this; ++*this; return ret; } constexpr mint operator--(int) { mint ret = *this; --*this; return ret; } constexpr mint& operator+=(const mint& that) { if ((val += that.val) >= MOD) {val -= MOD;} return *this; } constexpr mint& operator-=(const mint& that) { if ((val -= that.val) >= MOD) {val += MOD;} return *this; } constexpr mint& operator*=(const mint& that) { val = unsigned(ull(val) * that.val % MOD); return *this; } mint& operator/=(const mint& that) { return *this *= that.inv(); } constexpr mint operator+() const { return *this; } constexpr mint operator-() const { return raw(0) - *this; } constexpr mint operator+(const mint& that) const { return mint(*this) += that; } constexpr mint operator-(const mint& that) const { return mint(*this) -= that; } constexpr mint operator*(const mint& that) const { return mint(*this) *= that; } mint operator/(const mint& that) const { return mint(*this) /= that; } constexpr bool operator==(const mint& that) const { return val == that.val; } constexpr bool operator!=(const mint& that) const { return val != that.val; } // function mint& poweq(ll x) { // note: x is not mint mint a = *this; val = 1u; while (x) { if (x&1) *this *= a; a *= a; x >>= 1; } return *this; } mint pow(ll x) const { return mint(*this).poweq(x); } mint& inveq() { return poweq(MOD - 2); } mint inv() const { return pow(MOD - 2); } }; // input int N; int A[125252]; vi g[125252]; // prepare int order[125252]; int B[125252]; // dp data struct Data { mint X{mint::raw(1)}; // pattern num mint Y{mint::raw(1)}; // score sum for all pattern mint W{mint::raw(1)}; // pow(2, edge num) bool is_target = false; void addchild(const Data& child) { if (child.is_target) { // merge const mint childXW = child.X + child.W; Y = Y * childXW + X * child.Y; X *= childXW; } else { // cut Y *= child.W; X *= child.W; } // add edge W *= child.W * mint::raw(2); } }; // dp calc Data dfs(int v, int bef, int root) { Data dp{}; dp.is_target = B[v] <= B[root]; for (int to : g[v]) if (to != bef) { Data child = dfs(to, v, root); dp.addchild(child); } return dp; } int main(){ // input scanf("%d", &N); REP(i,N) scanf("%d", A+i); REP(i,N-1) { int u,v; scanf("%d%d", &u, &v); --u; --v; g[u].push_back(v); g[v].push_back(u); } // calc order iota(order, order+N, 0); sort(order, order+N, [](int i, int j){ if (A[i] != A[j]) { return A[i] < A[j]; } return i < j; }); REP(i,N)B[order[i]] = i; // calc dp mint ans = 0; REP(i,N){ Data dp = dfs(i, -1, i); ans += mint::raw(A[i]) * dp.Y; } printf("%u\n", ans.val); return 0; }