A Dynamical Model for the Orbit of the Andromeda Galaxy M31 and the Origin of the Local Group of Galaxies