Wrong output r[0] vs r[N-1]

Basically you are doing the work of LAMBDA but getting the security of LAMBDA-1.