#include <bits/stdc++.h>
#define N 500020
#define mod 1000000007
#define ll long long
using namespace std;
inline int read(){
int x=0,f=0;char ch=getchar();
while(ch>'9'||ch<'0'){if(ch=='-')f=1;ch=getchar();}
while(ch<='9'&&ch>='0'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return f?-x:x;
}
struct node{
int to, nxt;
}e[N<<1];
struct edge{
int val, id;
}a[N];
int head[N], cnt;
ll sz[N], vis[N], ans, val[N], fa[N];
void ins(int x, int y){
e[++cnt] = (node){y, head[x]}; head[x] = cnt;
e[++cnt] = (node){x, head[y]}; head[y] = cnt;
}
bool cmp1(edge a, edge b){
return a.val < b.val;
}
bool cmp2(edge a, edge b){
return a.val > b.val;
}
int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
void dfs(int x){
sz[x] = 0; ll sum = 0;
for(int i = head[x]; i; i = e[i].nxt){
if(!vis[e[i].to]) continue;
int y = find(e[i].to);
sz[x] += sz[fa[y]];
sum = (sum+sz[fa[y]]*sz[fa[y]])%mod;
fa[fa[x]] = x;
}
ans = (ans+(sz[x]*2+sz[x]*sz[x]-sum)%mod*val[x])%mod;
sz[x]++; vis[x] = 1;
}
int main(){
int n = read();
for(int i = 1; i <= n; i++) val[i] = a[a[i].id = i].val = read();
for(int i = 1; i < n; i++) ins(read(), read());
sort(a+1, a+n+1, cmp1);
for(int i = 1; i <= n; i++) fa[i] = i;
for(int i = 1; i <= n; i++)
dfs(a[i].id);
ll ans1 = ans; ans = 0;
memset(vis, 0, sizeof vis);
for(int i = 1; i <= n; i++) fa[i] = i;
for(int i = n; i; i--)
dfs(a[i].id);
printf("%d\n", ((ans1-ans)%mod+mod)%mod);
}