This is actually an invalid header yes, as the ending 255 in line 1500 1500 255 should be placed on a new line instead.
As there have been some (unofficial) extensions to the PNM format, allowing to store 3D volumetric images (those extensions are used by Pink for instance), G’MIC explicitely infers that the line 1500 1500 255 defines a 1500x1500x255 volumetric image, with no COLORMAX field defined, rather than assuming a 2D image of size 1500x1500 with a COLORMAX of 255.
I could change the way the PNM headers are parsed, and assume that a 3D volumetric image with no COLORMAX if probably more a 2D image with a COLORMAX though, but this could break the compatibility with other PNM files (3D volumetric images that do not define a COLORMAX).
EDIT:
I’ve read the old thread : 3D pgm output support
and indeed, Pink uses also P5 to define 3D volumetric images.
So, if do something special for the P5 generated by potrace, this will probably break the support of 3D volumetric images in Pink.