When you create a file or directory, you can define the permissions for the item you are going to create. These are the 0777 permissions given by You.
As creating a new directory is nothing else but writing an entry into the directory above, you need write permissions for /var/www/html/piRNA_html/UNAFold/output.
The user that needs those permissions is the cgi user, the script will run under.
Here You can find some hints on CGI programming