Groebner flags and Gorenstein algebras