Your missing one other scenario that should answer the question. If it goes into the endzone without going out of bounds it is a touchback. Hitting the pylon is hitting the endzone. Just like hitting the pylon is a touchdown with the football when attempting to score
I think one of the keys on this rule is the difference between possession/fumble and muffed punt. If it’s possession/fumble, it’s a safety. If it’s a muffed punt, it’s a touchback. I think both should be a safety, but I believe this has happened before and the precedent indicates this is a touchback.