#include #include #include #include using namespace std; using namespace atcoder; using namespace __gnu_pbds; using mint = modint998244353; #define Yes(n) cout << ((n) ? "Yes" : "No" ) << endl #define print(var) std::cout<<#var<<"="<<(var)< #define vvi vector #define vvvi vector #define ll long long #define vll vector #define vvll vector #define vvvll vector #define vvvvll vector #define vmi vector #define vvmi vector #define vvvmi vector #define vvvvmi vector #define vvvvvmi vector #define vs vector #define pii pair #define vpii vector #define vvpii vector #define bit(x,i)(((x)>>(i))&1) #define inf (1<<30) #define INF (1ll<<60) #define X first #define Y second template inline bool chmax(T &a, T b) { return ((a < b) ? (a = b, true) : (false)); } template inline bool chmin(T &a, T b) { return ((a > b) ? (a = b, true) : (false)); } template T nibutan(T ok, T ng, const F &f){while(abs(ok-ng)>1){T mid = (ok+ng)/2;(f(mid)?ok:ng) = mid;}return ok;} template vector digit(T x){vector res; while(x>0){res.push_back(x%10); x/=10;} return res;} ostream& operator<<(ostream& os, const mint& x){ os << x.val(); return os; } template istream &operator>>(istream &is, vector &vec){ for(auto &v:vec) is >> v; return is; } template void coutvector(vector x){ for(int i=0;i<(int)x.size();i++){if(i>0) cout<<" ";cout<, rb_tree_tag, tree_order_statistics_node_update>; template using Unordered_Map = gp_hash_table; #ifdef LOCAL #include CPP_DUMP_DEFINE_EXPORT_OBJECT(mint,val()); #else #define cpp_dump #endif int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin>>n; vll a(n); cin>>a; vvi g(n); for(int i=0;i>u>>v,u--,v--; g[u].push_back(v); g[v].push_back(u); } mint ans = 0; vmi dp(n,0); auto dfs = [&](auto dfs, int tp, int pa)->void{ mint tmp = 0; mint sum = 0; for(auto np:g[tp])if(np!=pa){ dfs(dfs,np,tp); dp[tp] += dp[np]+a[np]; tmp += (dp[np]+a[np])*(dp[np]+a[np]); sum += dp[np]+a[np]; } tmp = (sum*sum-tmp)/2; tmp *= a[tp]; ans += tmp; dp[tp]*=a[tp]; ans += dp[tp]; return; }; dfs(dfs,0,-1); cpp_dump(dp); cout << ans.val() << endl; }