<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def write_svg_debug(matrix, version, out, scale=15, border=None, fallback_color='fuchsia', color_mapping=None, add_legend=True):
 """\ Internal SVG serializer which is useful to debugging purposes. This function is not exposed to the QRCode class by intention and the resulting SVG document is very inefficient (lots of <rect/>s). Dark modules are black and light modules are white by default. Provide a custom `color_mapping` to override these defaults. Unknown modules are red by default. :param matrix: The matrix :param version: Version constant :param out: binary file-like object or file name :param scale: Scaling factor :param border: Quiet zone :param fallback_color: Color which is used for modules which are not 0x0 or 0x1 and for which no entry in `color_mapping` is defined. :param color_mapping: dict of module values to color mapping (optional) :param bool add_legend: Indicates if the bit values should be added to the matrix (default: True) """

    clr_mapping = {
        0x0: '#fff',
        0x1: '#000',
        0x2: 'red',
        0x3: 'orange',
        0x4: 'gold',
        0x5: 'green',
    }
    if color_mapping is not None:
        clr_mapping.update(color_mapping)
    border = get_border(version, border)
    width, height = get_symbol_size(version, scale, border)
    matrix_size = get_symbol_size(version, scale=1, border=0)[0]
    with writable(out, 'wt', encoding='utf-8') as f:
        legend = []
        write = f.write
        write('<?xml version="1.0" encoding="utf-8"?>\n')
        write('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 {0} {1}">'.format(width, height))
        write('<style type="text/css"><![CDATA[ text { font-size: 1px; font-family: Helvetica, Arial, sans; } ]]></style>')
        write('<g transform="scale({0})">'.format(scale))
        for i in range(matrix_size):
            y = i + border
            for j in range(matrix_size):
                x = j + border
                bit = matrix[i][j]
                if add_legend and bit not in (0x0, 0x1):
                    legend.append((x, y, bit))
                fill = clr_mapping.get(bit, fallback_color)
                write('<rect x="{0}" y="{1}" width="1" height="1" fill="{2}"/>'.format(x, y, fill))
        # legend may be empty if add_legend == False
        for x, y, val in legend:
            write('<text x="{0}" y="{1}">{2}</text>'.format(x+.2, y+.9, val))
        write('</g></svg>\n')