Refactor (do not split in several functions): import numpy as np
from PIL import Image

def adjust_grayscale_values(img: np.ndarray) -> tuple[np.ndarray, int, int]:
    """Adjust the values of a grayscale image."""
    try:
        # Calculate the number of pixels with values less than 250
        total_pixels_below_250 = np.sum(img < 250)

        # Calculate modal white value in the range [250, 256)
        hist_white, bins_white = np.histogram(img[img >= 250], bins=6, range=(250, 256))
        modal_white = int(bins_white[np.argmax(hist_white)])

        # Convert image to float for processing
        adjusted = img.astype(np.float64)

        # Create a mask for extreme original values (0 and 255)
        mask = (img == 0) | (img == 255)

        # Exclude extreme values from the white adjustment
        white_adjust_mask = ~mask

        # Scale image if modal white is not at maximum intensity
        if modal_white != 255:
            adjusted[white_adjust_mask] *= 255.0 / modal_white
            adjusted = np.clip(adjusted, 0, 255)

        # After white value adjustment, identify new extreme values
        new_extremes = (adjusted == 0) | (adjusted == 255)

        # Combine masks to exclude all extreme values from black adjustment
        total_extremes = mask | new_extremes
        black_adjust_mask = ~total_extremes

        # Initialize histogram range upper bound
        hist_range_upper = 61

        # Repeat adjustment until 0 is the largest peak in the histogram
        while True:
            # Calculate histogram for the current range [0, hist_range_upper)
            hist_black, bins_black = np.histogram(
                adjusted[adjusted < hist_range_upper], bins=hist_range_upper, range=(0, hist_range_upper)
            )
            threshold = total_pixels_below_250 * 0.01  # 1% of total pixels below 250

            # Find all peaks greater than the threshold
            peaks_black = np.where(hist_black > threshold)[0]

            if len(peaks_black) == 0:
                # If no peaks are found, break the loop
                break

            # Check if 0 is the largest peak
            if np.argmax(hist_black) == 0:
                break

            # Select first peak
            first_peak_index = peaks_black[0]
            modal_black = int(bins_black[first_peak_index])

            # Adjust image based on modal black value
            if modal_black:
                adjusted[black_adjust_mask] = (
                    adjusted[black_adjust_mask] - modal_black
                ) * (255.0 / (255 - modal_black))
                adjusted = np.clip(adjusted, 0, 255)

                gamma = 1.0 - (modal_black / 255.0)
                adjusted[black_adjust_mask] = (
                    np.power(adjusted[black_adjust_mask] / 255.0, gamma) * 255
                )

            # Reduce histogram upper bound by 5
            hist_range_upper = max(5, hist_range_upper - 5)

        # Round to nearest integer and then convert to uint8
        adjusted = np.rint(adjusted).astype(np.uint8)

        return adjusted, modal_black, modal_white

    except ValueError as e:
        print(f"Error adjusting values for image: {e}")
        return img, 0, 255