Firstly, you did the right thing and I'm sorry you had that experience. :(
Flashers get off on the reaction. They are mentally ill. They feel so insignificant that they just want any like scrap of attention. Anger, shock, disgust it's all why they do it.
On my first night in Korea I walked past this guy (fully dressed) but holding his junk and saying "hey , hey , hey "to me as I walked by - I knew what I just told you. So I pretended he was invisible and walked on. I was so lucky that I was in a big space and there were people about.