Luckily, flukily.
In a jammy or jamlike way.
By serendipity; by blind luck in combination with wisdom; by fortunate accident.