Implement a function count_paths_with_coins(N, K, arr) that counts the number of unique paths in a 2D grid from the top-left corner to the bottom-right corner, such that the total number of coins collected along the way equals exactly K.
Functionality Requirements:
The function takes three parameters:
N (int): The size of the grid (N x N).
K (int): The target number of coins to collect.
arr (list of list of int): A 2D list where each element represents the number of coins at that cell in the grid.
The movement is restricted to the right or downward.
The function returns an integer: the number of distinct paths where the sum of coins along the path from the top-left corner (0, 0) to the bottom-right corner (N-1, N-1) is exactly K.
Input/Output Requirements:
Input:
N: An integer where 1 ≤ N ≤ 100.
K: An integer where 0 ≤ K ≤ 1000.
arr: A list of lists of integers where 0 ≤ arr[i][j] ≤ 100 for all 0 ≤ i, j < N.
Output:
An integer representing the number of unique paths that collect exactly K coins.
Implementation Details:
Utilize memoization to optimize the path counting.
Ensure the implementation efficiently handles the maximum constraints outlined.
The solution must be written in a file called /app/solution.py.