An explanation for the muon and electron g − 2 anomalies and dark matter