← Home
 ```go
package main

import (
    "bufio"
    "fmt"
    "os"
    "strconv"
    "strings"
)

type Fenwick struct {
    tree []int
    n    int
}

func NewFenwick(n int) *Fenwick {
    return &Fenwick{
        tree: make([]int, n+2),
        n:    n,
    }
}

func (f *Fenwick) Update(i, delta int) {
    for i <= f.n {
        f.tree[i] += delta
        i += i & -i
    }
}

func (f *Fenwick) Query(i int) int {
    res := 0
    for i > 0 {
        res += f.tree[i]
        i -= i & -i
    }
    return res
}

func main() {
    reader := bufio.NewReader(os.Stdin)
    
    line, _ := reader.ReadString('\n')
    n, _ := strconv.Atoi(strings.TrimSpace(line))
    
    s, _ := reader.ReadString('\n')
    s = strings.TrimSpace(s)
    
    pos := make([][]int, 26)
    for i := 0; i < n; i++ {
        c := s[i] - 'a'
        pos[c] = append(pos[c], i)
    }
    
    p := make([]int, n)
    ptr := make([]int, 26)
    
    for i := 0; i < n; i++ {
        c := s[n-1-i] - 'a'
        p[i] = pos[c][ptr[c]] + 1
        ptr[c]++
    }
    
    ft := NewFenwick(n)
    var ans int64
    
    for i := n - 1; i >= 0; i-- {
        ans += int64(ft.Query(p[i] - 1))
        ft.Update(p[i], 1)
    }
    
    fmt.Println(ans)
}
```