Lights goes completely out and back on again and flashes for more than 20 seconds
That is a classic arc fault.  You need to stop that happening right away or you'll have ashes for a house. 

First, figure out which circuit is affected.  It's on the same circuit if turning off one branch circuit breaker turns off all the affected loads.  If it's on multiple circuits, then it's something to do with the panel or service.  

Confine your troubleshooting to places common to all affected areas.  For instance if a single circuit is involved, don't mess with outlets on any other circuit.  If multiple circuits are involved, don't mess with anything on any individual circuit; only look at places which serve multiple circuits.  

Madly replacing parts will not work, it's not a faulty part, it's a faulty connection.  Only replace parts after you find the bad connection and find it too badly burnt to continue in service.  You'll know it when you find it. 

On a single branch circuit, check the neutral bar in the panel - most overlooked. After the neutral bar, the likeliest place is a backstab connection at an outlet or switch.  Or a bad wire nut.  Wire nuts need to hold together on their own. If they need tape to stay together, they have been done wrong and will cause this.