{"trustable":true,"prependHtml":"\u003cstyle type\u003d\u0027text/css\u0027\u003e\n .input, .output {\n border: 1px solid #888888;\n }\n .output {\n margin-bottom: 1em;\n position: relative;\n top: -1px;\n }\n .output pre, .input pre {\n background-color: #EFEFEF;\n line-height: 1.25em;\n margin: 0;\n padding: 0.25em;\n }\n \u003c/style\u003e\n \u003clink rel\u003d\"stylesheet\" href\u003d\"//codeforces.org/s/96598/css/problem-statement.css\" type\u003d\"text/css\" /\u003e\u003cscript\u003e window.katexOptions \u003d { disable: true }; \u003c/script\u003e\n\u003cscript type\u003d\"text/x-mathjax-config\"\u003e\n MathJax.Hub.Config({\n tex2jax: {\n inlineMath: [[\u0027$$$\u0027,\u0027$$$\u0027], [\u0027$\u0027,\u0027$\u0027]],\n displayMath: [[\u0027$$$$$$\u0027,\u0027$$$$$$\u0027], [\u0027$$\u0027,\u0027$$\u0027]]\n }\n });\n\u003c/script\u003e\n\u003cscript type\u003d\"text/javascript\" async src\u003d\"https://mathjax.codeforces.org/MathJax.js?config\u003dTeX-AMS_HTML-full\"\u003e\u003c/script\u003e","sections":[{"title":"","value":{"format":"HTML","content":"\u003cp\u003eGiven a tree with $$$n$$$ nodes numbered from $$$1$$$ to $$$n$$$. Each node $$$i$$$ has an associated value $$$V_i$$$.\u003c/p\u003e\u003cp\u003eIf the simple path from $$$u_1$$$ to $$$u_m$$$ consists of $$$m$$$ nodes namely $$$u_1 \\rightarrow u_2 \\rightarrow u_3 \\rightarrow \\dots u_{m-1} \\rightarrow u_{m}$$$, then its alternating function $$$A(u_{1},u_{m})$$$ is defined as $$$A(u_{1},u_{m}) \u003d \\sum\\limits_{i\u003d1}^{m} (-1)^{i+1} \\cdot V_{u_{i}}$$$. A path can also have $$$0$$$ edges, i.e. $$$u_{1}\u003du_{m}$$$.\u003c/p\u003e\u003cp\u003eCompute the sum of alternating functions of all unique simple paths. Note that the paths are directed: two paths are considered different if the starting vertices differ or the ending vertices differ. The answer may be large so compute it modulo $$$10^{9}+7$$$. \u003c/p\u003e"}},{"title":"Input","value":{"format":"HTML","content":"\u003cp\u003eThe first line contains an integer $$$n$$$ $$$(2 \\leq n \\leq 2\\cdot10^{5} )$$$ — the number of vertices in the tree.\u003c/p\u003e\u003cp\u003eThe second line contains $$$n$$$ space-separated integers $$$V_1, V_2, \\ldots, V_n$$$ ($$$-10^9\\leq V_i \\leq 10^9$$$) — values of the nodes.\u003c/p\u003e\u003cp\u003eThe next $$$n-1$$$ lines each contain two space-separated integers $$$u$$$ and $$$v$$$ $$$(1\\leq u, v\\leq n, u \\neq v)$$$ denoting an edge between vertices $$$u$$$ and $$$v$$$. It is guaranteed that the given graph is a tree.\u003c/p\u003e"}},{"title":"Output","value":{"format":"HTML","content":"\u003cp\u003ePrint the total sum of alternating functions of all unique simple paths modulo $$$10^{9}+7$$$. \u003c/p\u003e"}},{"title":"Examples","value":{"format":"HTML","content":"\u003ctable class\u003d\u0027vjudge_sample\u0027\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e4\n-4 1 5 -2\n1 2\n1 3\n1 4\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e40\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n"}},{"title":"","value":{"format":"HTML","content":"\u003ctable class\u003d\u0027vjudge_sample\u0027\u003e\n\u003cthead\u003e\n \u003ctr\u003e\n \u003cth\u003eInput\u003c/th\u003e\n \u003cth\u003eOutput\u003c/th\u003e\n \u003c/tr\u003e\n\u003c/thead\u003e\n\u003ctbody\u003e\n \u003ctr\u003e\n \u003ctd\u003e\u003cpre\u003e8\n-2 6 -4 -4 -9 -3 -7 23\n8 2\n2 3\n1 4\n6 5\n7 6\n4 7\n5 8\n\u003c/pre\u003e\u003c/td\u003e\n \u003ctd\u003e\u003cpre\u003e4\n\u003c/pre\u003e\u003c/td\u003e\n \u003c/tr\u003e\n\u003c/tbody\u003e\n\u003c/table\u003e\n"}},{"title":"Note","value":{"format":"HTML","content":"\u003cp\u003eConsider the first example.\u003c/p\u003e\u003cp\u003eA simple path from node $$$1$$$ to node $$$2$$$: $$$1 \\rightarrow 2$$$ has alternating function equal to $$$A(1,2) \u003d 1 \\cdot (-4)+(-1) \\cdot 1 \u003d -5$$$.\u003c/p\u003e\u003cp\u003eA simple path from node $$$1$$$ to node $$$3$$$: $$$1 \\rightarrow 3$$$ has alternating function equal to $$$A(1,3) \u003d 1 \\cdot (-4)+(-1) \\cdot 5 \u003d -9$$$.\u003c/p\u003e\u003cp\u003eA simple path from node $$$2$$$ to node $$$4$$$: $$$2 \\rightarrow 1 \\rightarrow 4$$$ has alternating function $$$A(2,4) \u003d 1 \\cdot (1)+(-1) \\cdot (-4)+1 \\cdot (-2) \u003d 3$$$.\u003c/p\u003e\u003cp\u003eA simple path from node $$$1$$$ to node $$$1$$$ has a single node $$$1$$$, so $$$A(1,1) \u003d 1 \\cdot (-4) \u003d -4$$$.\u003c/p\u003e\u003cp\u003eSimilarly, $$$A(2, 1) \u003d 5$$$, $$$A(3, 1) \u003d 9$$$, $$$A(4, 2) \u003d 3$$$, $$$A(1, 4) \u003d -2$$$, $$$A(4, 1) \u003d 2$$$, $$$A(2, 2) \u003d 1$$$, $$$A(3, 3) \u003d 5$$$, $$$A(4, 4) \u003d -2$$$, $$$A(3, 4) \u003d 7$$$, $$$A(4, 3) \u003d 7$$$, $$$A(2, 3) \u003d 10$$$, $$$A(3, 2) \u003d 10$$$. So the answer is $$$(-5) + (-9) + 3 + (-4) + 5 + 9 + 3 + (-2) + 2 + 1 + 5 + (-2) + 7 + 7 + 10 + 10 \u003d 40$$$.\u003c/p\u003e\u003cp\u003eSimilarly $$$A(1,4)\u003d-2, A(2,2)\u003d1, A(2,1)\u003d5, A(2,3)\u003d10, A(3,3)\u003d5, A(3,1)\u003d9, A(3,2)\u003d10, A(3,4)\u003d7, A(4,4)\u003d-2, A(4,1)\u003d2, A(4,2)\u003d3 , A(4,3)\u003d7$$$ which sums upto 40. \u003c/p\u003e"}}]}