The problem is very possibly a loose cable connection from the upper half of the 3ds to the motherboard, since if the 3ds identifies that it is not connected to stuff on the upper half(cameras, 3d slider, screen), it would just forcefully shut down. Mine had the same problem a few weeks ago. In my case, it was a torn 3d slider cable, and it just won't turn on no matter what I do. However, when I swapped the old cable with a replacement, everything was back to normal. I don't think that your 3ds have anything torn, since it works just fine if the screen don't lay totally flat. In that case you'll just have to reattach the cables so that it doesn't disconnect. Here's the guide for it.
Nintendo 3DS Motherboard Replacement Guide
You only have to reattatch the cables since the motherboard itself have no obvious issues. However, be careful with the sockets of the cables, as they're very fragile. Good luck!
crwdns2934105:0crwdne2934105:0
crwdns2934113:0crwdne2934113:0
crwdns2915270:0crwdne2915270:0
crwdns2889612:0crwdne2889612:0
0