def get_valid_cell_mapping(valid_cell_numbers):
    """Calculate which source cells are needed for each target cell.

    This returns a mapping: target cells -> source cells (indices), with source and
    target cell indices starting at 0, i.e. at the first valid target/source cells!

    Raises:
        ValueError: If the first or last valid cell number is None.
        ValueError: If the first valid cell number is 0.

    """
    if valid_cell_numbers[0] is None or valid_cell_numbers[-1] is None:
        raise ValueError("First and last valid cell number must be valid.")
    if valid_cell_numbers[0] == 0:
        raise ValueError("First valid cell number must be non-zero.")

    mapping = {}
    curr_tgt_cells = []
    curr_src_cells = []
    src_cell_count = 0
    for tgt_index, cell_number in enumerate(valid_cell_numbers):
        if cell_number and curr_tgt_cells:
            # Write current state to the mapping and reset.
            mapping[tuple(curr_tgt_cells)] = tuple(curr_src_cells)
            curr_tgt_cells = []
            curr_src_cells = []

        curr_tgt_cells.append(tgt_index)
        for _ in range(cell_number):
            curr_src_cells.append(src_cell_count)
            src_cell_count += 1

    # Finally write the last state to the mapping.
    mapping[tuple(curr_tgt_cells)] = tuple(curr_src_cells)

    return mapping