from typing import Sequence


def _count_run(li: Sequence, lo: int, hi: int) -> int:
    """Count the length of the run beginning at lo, in the slice [lo, hi).
    
    lo < hi is required on entry.
    """
    # "A run" is either the longest non-decreasing sequence, or the longest strictly 
    # decreasing sequence. `descending` is False in the former case, True in the latter.
    # Note: This function is not required by tim_sort(), so we make it internal.
    assert lo < hi
    # descending = False
    lo += 1
    if lo == hi:
        return 1
    
    n = 2  # run count
    if li[lo] < li[lo-1]:
        # descending = True
        for lo in range(lo+1, hi):
            if li[lo] >= li[lo-1]:
                break
            n += 1
    else:
        for lo in range(lo+1, hi):
            if li[lo] < li[lo-1]:
                break
            n += 1
    
    return n