I had the exact same problem on an old Mazda i had years ago... it ended up being a faulty coil or ballast resistor (i replaced both at same time so not sure what fixed problem). It had me stumped for ages but that sorted it out (after replacing 1/2 the electrics/motor)

Could also be a faulty immobilisor/kill switch.