The answer is pretty simple.
The domain name yahoo.comyahoo.comoo.com
belongs to the owners of comoo.com
, and so those people can do whatever they want with it. The reason that yahoo.comyahoo.comoo.com
returns an MX record is that the owners of comoo.com
decided to make it do that.
Likewise, the domain name gmail.com.com.com.com
belongs to the owners of com.com
, and so those people can do whatever they want with it. They have decided to make gmail.com.com.com.com
return an MX record.
I'm not sure why you think these domain names are invalid, or that there's some kind of problem here. These domain names are completely valid and there's no problem. Everything is working exactly as it's supposed to.
Perhaps you're thinking that the first part of a domain name is the most significant part and the last parts are not so significant. Actually, the opposite is true: the last parts are the most significant and the first part is the least significant.
It's just like how if you send a letter to Michigan City, Indiana, then it will go to the state of Indiana, not the state of Michigan. The fact that the address starts with the word "Michigan" doesn't matter, because the most significant part of the address is the end, not the beginning.