package main import ( "bufio" "fmt" "os" "sort" "strconv" ) func getNextString(scanner *bufio.Scanner) string { if !scanner.Scan() { panic("scan failed") } return scanner.Text() } func atoi(s string) int { x, _ := strconv.Atoi(s); return x } func getNextInt(scanner *bufio.Scanner) int { return atoi(getNextString(scanner)) } func atoi64(s string) int64 { x, _ := strconv.ParseInt(s, 10, 64); return x } func getNextInt64(scanner *bufio.Scanner) int64 { return atoi64(getNextString(scanner)) } func atof64(s string) float64 { x, _ := strconv.ParseFloat(s, 64); return x } func getNextFloat64(scanner *bufio.Scanner) float64 { return atof64(getNextString(scanner)) } func main() { fp := os.Stdin wfp := os.Stdout extra := 0 if os.Getenv("I") == "IronMan" { fp, _ = os.Open(os.Getenv("END_GAME")) extra = 100 } scanner := bufio.NewScanner(fp) scanner.Split(bufio.ScanWords) scanner.Buffer(make([]byte, 1000005), 1000005) writer := bufio.NewWriter(wfp) defer func() { r := recover() if r != nil { fmt.Fprintln(writer, r) } writer.Flush() }() solve(scanner, writer) for i := 0; i < extra; i++ { fmt.Fprintln(writer, "-----------------------------------") solve(scanner, writer) } } func solve(scanner *bufio.Scanner, writer *bufio.Writer) { SetMod(Mod998244353) n := getNextInt(scanner) type pair [2]int aa := make([]pair, n) for i := 0; i < n; i++ { aa[i][0] = i aa[i][1] = getNextInt(scanner) } sort.Slice(aa, func(i, j int) bool { return aa[i][1] < aa[j][1] }) cnt := NewFenwickTree(n) sum := NewFenwickTree(n) lcnt := make([]Mint, n) rcnt := make([]Mint, n) lsum := make([]Mint, n) rsum := make([]Mint, n) l := 0 for i := 0; i < n; i++ { for aa[l][1] < aa[i][1] { cnt.Add(aa[l][0], 1) sum.Add(aa[l][0], int64(aa[l][1])) l++ } rcnt[aa[i][0]] = Mint(cnt.Sum(aa[i][0]+1, n)).Mod() rsum[aa[i][0]] = Mint(sum.Sum(aa[i][0]+1, n)).Mod() } sort.Slice(aa, func(i, j int) bool { return aa[i][1] > aa[j][1] }) cnt = NewFenwickTree(n) sum = NewFenwickTree(n) l = 0 for i := 0; i < n; i++ { for aa[l][1] > aa[i][1] { cnt.Add(aa[l][0], 1) sum.Add(aa[l][0], int64(aa[l][1])) l++ } lcnt[aa[i][0]] = Mint(cnt.Sum(0, aa[i][0])).Mod() lsum[aa[i][0]] = Mint(sum.Sum(0, aa[i][0])).Mod() } var ans Mint for i := 0; i < n; i++ { ans.AddAs(lcnt[aa[i][0]].Mul(rsum[aa[i][0]])) ans.AddAs(rcnt[aa[i][0]].Mul(lsum[aa[i][0]])) ans.AddAs(rcnt[aa[i][0]].Mul(lcnt[aa[i][0]]).Mul(Mint(aa[i][1]))) } fmt.Fprintln(writer, ans) } // Mod constants. const ( Mod1000000007 = 1000000007 Mod998244353 = 998244353 ) var ( mod Mint fmod func(Mint) Mint ) // Mint treats the modular arithmetic type Mint int64 // SetMod sets the mod. It must be called first. func SetMod(newmod Mint) { switch newmod { case Mod1000000007: fmod = staticMod1000000007 case Mod998244353: fmod = staticMod998244353 default: mod = newmod fmod = dynamicMod } } func dynamicMod(m Mint) Mint { m %= mod if m < 0 { return m + mod } return m } func staticMod1000000007(m Mint) Mint { m %= Mod1000000007 if m < 0 { return m + Mod1000000007 } return m } func staticMod998244353(m Mint) Mint { m %= Mod998244353 if m < 0 { return m + Mod998244353 } return m } // Mod returns m % mod. func (m Mint) Mod() Mint { return fmod(m) } // Inv returns modular multiplicative inverse func (m Mint) Inv() Mint { return m.Pow(Mint(0).Sub(2)) } // Pow returns m^n func (m Mint) Pow(n Mint) Mint { p := Mint(1) for n > 0 { if n&1 == 1 { p.MulAs(m) } m.MulAs(m) n >>= 1 } return p } // Add returns m+x func (m Mint) Add(x Mint) Mint { return (m + x).Mod() } // Sub returns m-x func (m Mint) Sub(x Mint) Mint { return (m - x).Mod() } // Mul returns m*x func (m Mint) Mul(x Mint) Mint { return (m * x).Mod() } // Div returns m/x func (m Mint) Div(x Mint) Mint { return m.Mul(x.Inv()) } // AddAs assigns *m + x to *m and returns m func (m *Mint) AddAs(x Mint) *Mint { *m = m.Add(x) return m } // SubAs assigns *m - x to *m and returns m func (m *Mint) SubAs(x Mint) *Mint { *m = m.Sub(x) return m } // MulAs assigns *m * x to *m and returns m func (m *Mint) MulAs(x Mint) *Mint { *m = m.Mul(x) return m } // DivAs assigns *m / x to *m and returns m func (m *Mint) DivAs(x Mint) *Mint { *m = m.Div(x) return m } // FenwickTree Data structure that can efficiently update elements and calculate prefix sums in a table of numbers. type FenwickTree struct { n int data []int64 } // NewFenwickTree Constructor func NewFenwickTree(n int) *FenwickTree { data := make([]int64, n+1) return &FenwickTree{n: n, data: data} } // Add processes a[p] += x. func (f *FenwickTree) Add(p int, x int64) { for p++; p <= f.n; p += p & -p { f.data[p] += x } } // Sum returns a[l] + a[l - 1] + ... + a[r - 1]. func (f *FenwickTree) Sum(l, r int) int64 { return f.sum(r) - f.sum(l) } func (f *FenwickTree) sum(r int) int64 { s := int64(0) for ; r > 0; r -= r & -r { s += f.data[r] } return s }