Differ from other main-sequence stars, hundreds of planets discovered around M dwarfs are in close-in orbits with plenty of them embedded in tightly compact multi-planetary systems. Most planets are terrestrial planets explored in the high-levels of X-ray and ultraviolet radiation because of the stellar magnetic activity. To demonstrate the formation of terrestrial planets around M dwarfs, several mechanisms have been proposed, including in-situ formation, inside-out formation and inward/convergent migration formation scenario. Here we perform N-body simulations of planetesimal accretion in three models of in-situ, type I inward and convergent migration to investigate the terrestrial planet formation around M dwarfs. Results show that in-situ formation could produce 7.77+3.23-3.77 small planets with an average mass of 1.23+4.01-0.93 M⊕ around M dwarfs. The number of planets tends to increase with the disk slope steeper and stellar mass. While 2.55+1.45-1.55 and 2.85+1.15-0.85 planets with larger masses of 3.76+8.77-3.46 and 3.01+13.77-2.71 M⊕ would be formed under inward and reversed migration scenario, respectively, because of the more efficient accretion rate. Migration scenario also deliver plentiful water from exterior of ice line to interior to form habitable planets. The orbital outcomes of reversed migration produce the best matching with observations. Besides, we also investigate the dynamical evolution of terrestrial planets under the influence of one giant planet. Results suggest that the mass and eccentricity of the giant planet may play a crucial role in shaping the final configuration of the system. High-order mean motion resonances (MMRs), for instance, 5:3 or 7:4 MMRs could be formed if the giant planet revolves the central star with an eccentric orbit. The eccentricity of terrestrial planets can be excited remarkably under the secular perturbation of giant planet and gaseous disk. Our work suggests a possible mechanism for the planet formation around M dwarfs.