#include <bits/stdc++.h>
using namespace std;
const int N = 1000010;
const int mod = 998244353;
const int inv = 332748118;
int n;
long long ans, js[N], ni[N];
inline long long C(int x, int y) {
  if (x < 0 || y < 0 || x < y) return 0;
  return js[x] * ni[y] % mod * ni[x - y] % mod;
}
inline long long qpow(long long x, long long y, long long ans = 1) {
  for (; y; y >>= 1, x = x * x % mod)
    if (y & 1) ans = ans * x % mod;
  return ans;
}
int main() {
  cin >> n;
  for (int i = js[0] = 1; i <= n; ++i) js[i] = js[i - 1] * i % mod;
  for (int i = (ni[n] = qpow(js[n], mod - 2), n); i; --i)
    ni[i - 1] = ni[i] * i % mod;
  for (int i = 0; i <= n; ++i) {
    long long f;
    if (!i)
      f = qpow(qpow(3, n) - 3, n);
    else
      f = ((i & 1) ? -1 : 1) * C(n, i) *
          (3 * qpow(qpow(3, n - i) - 1, n) % mod +
           (qpow(3, i) - 3) * qpow(3, 1ll * (n - i) * n) % mod) %
          mod;
    (ans += f) %= mod;
  }
  cout << ((qpow(3, 1ll * n * n % (mod - 1)) - ans) % mod + mod) % mod << endl;
  return 0;
}
