Romania has a long history dating back to ancient times. It was inhabited by Dacians in ancient times and became part of the Roman Empire in the 1st century AD. Several medieval states emerged on Romanian territories between the 13th-15th centuries. The modern Romanian state was formed in 1859 through the union of Moldavia and Wallachia. Romania gained independence in 1878 and a monarchy was established, lasting until 1947 when Romania became a communist state. Communism ended in 1989 and Romania transitioned to democracy and became a member of NATO and the EU in 2004 and 2007 respectively.