import bisect


def rightmostBinSearch(vec, val):
    """
    Return the rightmost position in the vector vec of val. If val is
    absent then we return the leftmost position of the value:
    min(vec[vec > val]). If val is greater than all elements in vec we
    return len(vec).
    """
    assert(len(vec) > 0)

    i = bisect.bisect_left(vec, val)

    if (len(vec) == i):
        return(i)

    while (i + 1 < len(vec) and vec[i + 1] == val):
        i += 1

    return(i)