import math


def constrained_factorial(x):
    """
    Same as `math.factorial`, but raises `ValueError` if x is under 0 or over 32,767.
    """
    if not (0 <= x < 32768):
        raise ValueError(f"{x!r} not in working 0-32,767 range")
    if math.isclose(x, int(x), abs_tol=1e-12):
        x = int(round(x))
    return math.factorial(x)