'c++ for coin combination problem not working

I am solving the coin combination problem given below:

Consider a money system consisting of n coins. Each coin has a positive integer value. Your task is to calculate the number of distinct ordered ways you can produce a money sum x using the available coins.

For example, if the coins are {2,3,5} and the desired sum is 9, there are 3 ways:

  • 2+2+5
  • 3+3+3
  • 2+2+2+3

Input:

The first input line has two integers n and x: the number of coins and the desired sum of money.

The second line has n distinct integers c1,c2,…,cn: the value of each coin.

Output:

Print one integer: the number of ways modulo 109+7.

For more information here is the link to the given problem: https://cses.fi/problemset/task/1636

I tried writing the following code in C++ using dynamic programming:

#include<bits/stdc++.h>
using namespace std;
 
const long N=1e6+10,mod=1e9+7;
long dp[N];
 
long recursion(int coins,int sum,int a[])
{
    if(sum==0)
        return 1;
    if(dp[sum]!=-1)
        return dp[sum];
    long count=0;
    for(long i=0;i<coins;i++)
    {
        if(sum-a[i]>=0)
            count=count+recursion(coins,sum-a[i],a);
    }
    return dp[sum]=count%mod;
} 
 
int main()
{
    memset(dp,-1,sizeof(dp));
    int n,x;
    cin>>n>>x;
    int a[n];
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
    }
    cout<<recursion(n,x,a);
    return 0;
}

But it shows wrong answer for some test cases. Can someone please help me sort this problem.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source