Thanks. I get some wrong results:

```
a=[1, 1] x=0 expect=0 result=1
a=[1, 1, 1] x=0 expect=0 result=1
a=[1, 1, 2] x=0 expect=0 result=1
a=[1, 2, 2] x=0 expect=0 result=1
a=[2, 2, 2] x=0 expect=0 result=1
a=[2, 2, 2] x=1 expect=0 result=1
a=[1, 1, 1, 1] x=0 expect=0 result=1
a=[1, 1, 1, 2] x=0 expect=0 result=1
a=[1, 1, 1, 3] x=0 expect=0 result=1
a=[1, 1, 2, 2] x=0 expect=0 result=1
a=[1, 1, 2, 3] x=0 expect=0 result=1
a=[1, 1, 3, 3] x=0 expect=0 result=1
a=[1, 2, 2, 2] x=0 expect=0 result=1
a=[1, 2, 2, 3] x=0 expect=0 result=1
a=[1, 2, 3, 3] x=0 expect=0 result=1
a=[1, 3, 3, 3] x=0 expect=0 result=1
a=[2, 2, 2, 2] x=0 expect=0 result=1
a=[2, 2, 2, 2] x=1 expect=0 result=1
a=[2, 2, 2, 3] x=0 expect=0 result=1
a=[2, 2, 2, 3] x=1 expect=0 result=1
a=[2, 2, 3, 3] x=0 expect=0 result=1
a=[2, 2, 3, 3] x=1 expect=0 result=1
a=[2, 3, 3, 3] x=0 expect=0 result=1
a=[2, 3, 3, 3] x=1 expect=0 result=1
a=[3, 3, 3, 3] x=0 expect=0 result=1
a=[3, 3, 3, 3] x=1 expect=0 result=1
a=[3, 3, 3, 3] x=2 expect=0 result=1
```

##
test script

```
from bisect import bisect_right as old
from itertools import combinations_with_replacement
def new(a, x, lo=0, hi=None, *, key=None):
"""Return the index where to insert item x in list a, assuming a is sorted.
The return value i is such that all e in a[:i] have e <= x, and all e in
a[i:] have e > x. So if x already appears in the list, a.insert(i, x) will
insert just after the rightmost x already there.
Optional args lo (default 0) and hi (default len(a)) bound the
slice of a to be searched.
A custom key function can be supplied to customize the sort order.
"""
if lo < 0:
raise ValueError('lo must be non-negative')
if hi is None:
hi = len(a)
# Note, the comparison uses "<" to match the
# __lt__() logic in list.sort() and in heapq.
mid = (lo + hi) // 2
if key is None:
while lo < mid:
if x < a[mid]:
hi = mid
else:
lo = mid
mid = (lo + hi) // 2
else:
while lo < mid:
if x < key(a[mid]):
hi = mid
else:
lo = mid
mid = (lo + hi) // 2
return hi
for n in range(5):
for a in map(list, combinations_with_replacement(range(n), n)):
for x in range(n+1):
expect = old(a, x)
result = new(a, x)
if result != expect:
print(f'{a=} {x=} {expect=} {result=}')
print('done')
```

Attempt This Online!