LL query(int x){ LL s = 0; while (x) { s += t[x]; x -= lowbit(x); } return s; }
voidmodify(int x, LL d){ while (x <= n) { t[x] += d; x += lowbit(x); } return ; }
intmain(){ read(n); scanf("%s", s + 1); for (int i = 1; i <= n; ++i) { a[i] = s[i] - 'a' + 1; b[n - i + 1] = a[i]; } for (int i = 1; i <= n; ++i) v[b[i]].push_back(i); for (int i = 1; i <= n; ++i) c[i] = v[a[i]][m[a[i]]++]; for (int i = 1; i <= n; ++i) { modify(c[i], 1); ans += i - query(c[i]); } printf("%lld\n", ans); return0; }