The touching by A in the endzone is ignored by rule. So B can not elect to take the ball at the spot of touching and get a TD, as there was no touching.
Ball stays alive in the endzone.
It's a free-for-all!
It would be extremely unlikely for that ball to come to rest without anyone trying to secure it, so give it a couple of seconds, and you'll know if you have a safety or a touchdown.