Write a Python program that implements a simple neural network for digit recognition using the MNIST dataset. The program should include training, validation, and testing phases, as well as an option to visualize the learned weights.

To implement a simple neural network for digit recognition using the MNIST dataset, we can use TensorFlow and Keras libraries. Here's an example of how to create such a program:

```python
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.utils import to_categorical

# Load the MNIST dataset
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()

# Preprocess the data
train_images = train_images.reshape((60000, 28 * 28))
train_images = train_images.astype('float32') / 255

test_images = test_images.reshape((10000, 28 * 28))
test_images = test_images.astype('float32') / 255

train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)

# Split training data into training and validation sets
val_images = train_images[:10000]
partial_train_images = train_images[10000:]

val_labels = train_labels[:10000]
partial_train_labels = train_labels[10000:]

# Create the neural network model
model = Sequential()
model.add(Dense(128, activation='relu', input_shape=(28 * 28,)))
model.add(Dense(64, activation='relu'))
model.add(Dense(10, activation='softmax'))

# Compile the model
model.compile(optimizer='adam',
              loss='categorical_crossentropy',
              metrics=['accuracy'])

# Train the model
history = model.fit(partial_train_images,
                    partial_train_labels,
                    epochs=20,
                    batch_size=128,
                    validation_data=(val_images, val_labels))

# Test the model
test_loss, test_acc = model.evaluate(test_images, test_labels)
print(f'Test accuracy: {test_acc}')

# Visualize the learned weights
layer_weights = model.layers[0].get_weights()[0]
fig, axes = plt.subplots(4, 8)
for i in range(32):
    ax = axes[i // 8, i % 8]
    ax.imshow(layer_weights[:, i].reshape(28, 28), cmap='gray')
    ax.axis('off')

plt.show()
```

This program creates a simple neural network with two hidden layers and trains it on the MNIST dataset. It also includes training, validation, and testing phases. The learned weights of the first layer are visualized using matplotlib.

Please note that you'll need to install TensorFlow and Keras libraries if you haven't already:

```bash
pip install tensorflow
```